Upgrade to Pro — share decks privately, control downloads, hide ads and more …

АФТИ ООП 2013-2014. Лекция II/2

АФТИ ООП 2013-2014. Лекция II/2

Продвинутые трюки с битами: http://www.williamspublishing.com/Books/978-5-8459-1838-3.html

Более простое: http://www.ozon.ru/context/detail/id/125884/

Oleg Dashevskii

February 24, 2014
Tweet

More Decks by Oleg Dashevskii

Other Decks in Education

Transcript

  1. Свободен Свободен data[0] data[1] data[2] Блок 0 data[3] ! data[4]

    data[5] data[6] data[7] Блок 1 data[8] ! data[9] Свободен Свободен Свободен Блок 2 Свободен Блок 0 Блок 1 Блок 2 Свободен Таблица указателей start
  2. • Вставка/удаление в любом месте обходится недорого. • Индексирование за

    O(1). • Деталь реализации: при инициализации всегда создается три блока, чтобы было куда расти. • http://www.codeproject.com/Articles/5425/An-In- Depth-Study-of-the-STL-Deque-Container
  3. template<typename _Tp, typename _Sequence = deque<_Tp> >! class stack {!

    public:! typedef typename _Sequence::value_type value_type;! typedef typename _Sequence::reference reference;! typedef typename _Sequence::const_reference const_reference;! typedef typename _Sequence::size_type size_type;! ! protected:! _Sequence c;! ! public:! explicit stack(const _Sequence& __c = _Sequence()) : c(__c) { }! ! bool empty() const { return c.empty(); } ! size_type size() const { return c.size(); }! reference top() { return c.back(); }! const_reference top() const { return c.back(); }! ! void push(const value_type& __x) { c.push_back(__x); }! void pop() { c.pop_back(); }! };!
  4. template<typename _Tp, typename _Sequence = deque<_Tp> >! class queue! {!

    public:! typedef typename _Sequence::value_type value_type;! typedef typename _Sequence::reference reference;! typedef typename _Sequence::const_reference const_reference;! typedef typename _Sequence::size_type size_type;! typedef _Sequence container_type;! ! protected:! _Sequence c;! ! public:! explicit queue(const _Sequence& __c = _Sequence()) : c(__c) {}! ! bool empty() const { return c.empty(); }! size_type size() const { return c.size(); }! ! reference front() { return c.front(); }! const_reference front() const { return c.front(); }! ! reference back() { return c.back(); }! const_reference back() const { return c.back(); }! ! void push(const value_type& __x) { c.push_back(__x); }! void pop() { c.pop_front(); }! };!
  5. template<typename _Tp, typename _Sequence = vector<_Tp>,! typename _Compare = less<typename

    _Sequence::value_type> >! class priority_queue {! public:! typedef typename _Sequence::value_type value_type;! typedef typename _Sequence::reference reference;! typedef typename _Sequence::const_reference const_reference;! typedef typename _Sequence::size_type size_type;! typedef _Sequence container_type;! protected:! _Sequence c;! _Compare comp;! public:! explicit priority_queue(const _Compare& __x = _Compare(),! const _Sequence& __s = _Sequence())! : c(__s), comp(__x)! { std::make_heap(c.begin(), c.end(), comp); }! ! bool empty() const { return c.empty(); }! size_type size() const { return c.size(); }! ! const_reference top() const { return c.front(); }! ! void push(const value_type& __x) {! c.push_back(__x);! std::push_heap(c.begin(), c.end(), comp);! }! ! void pop() {! std::pop_heap(c.begin(), c.end(), comp);! c.pop_back();! }! };!
  6. UNORDERED_MAP • Реализация ассоциативного массива на базе хеш- таблицы. •

    В GNU C++ используется реализация «таблица списков». • В Visual C++ один большой список, а в таблице хранятся пары итераторов (начало, конец) для соответствующей ячейки.
  7. template < class Key,! class T,! class Hash = hash<Key>,!

    class Pred = equal_to<Key>,! class Alloc = allocator< pair<const Key,T> >! > class unordered_map;! template<typename T>! struct hash;! ! template<>! struct hash<int> : public std::unary_function<int, std::size_t> {! std::size_t operator()(int __val) const {! return static_cast<std::size_t>(__val);! }! };! ! // .....! ! template <class _Tp>! struct equal_to : public binary_function<_Tp, _Tp, bool> {! bool operator()(const _Tp& __x, const _Tp& __y) const! { return __x == __y; }! };!
  8. ПРОЧИЕ КЛАССЫ • set<Key> – примерный аналог map<Key, bool> •

    Аналогично c multiset<Key>. • unordered_set, unordered_multiset. • bitset – битовый массив. • forward_list – односвязный список.
  9. vector<int>::const_iterator it;! ! for (it = numbers.begin(); it != numbers.end();

    ++it)! ! cout << *it << ' ';! vector<int>::const_reverse_iterator it;! ! for (it = numbers.rbegin(); it != numbers.rend(); ++it)! ! cout << *it << ' ';!
  10. template<class InputIterator, class OutputIterator>! OutputIterator copy(InputIterator first, InputIterator last,! OutputIterator

    result)! {! while (first != last) {! *result = *first;! ++result; ++first;! }! ! return result;! }!
  11. void print_number(int i) {! cout << i << ' ';!

    }! ! for_each(numbers.begin(), numbers.end(),! print_number);!
  12. bool is_odd(int i) { return i % 2 == 1;

    }! ! std::vector<int>::iterator bound;! bound = std::partition(numbers.begin(), numbers.end(),! is_odd);! ! // std::stable_partition(...)
  13. std::sort(numbers.begin(), numbers.end());! ! /**********************/! ! struct Person {! int age;!

    };! ! bool age_compare(const Person &p1, const Person &p2)! {! return p1.age < p2.age;! }! ! std::vector<Person> people;! std::sort(people.begin(), people.end(), age_compare);! ! // std::stable_sort(...)