containers C++ 【updating】

我写的这个系列强调如何通过C++来使用这些container,适合对数据结构有基本认识正在刷题的人使用。

Iterators

begin, end, rbegin, rend, cbegin, cend, crbegin, crend

begin - returns the beginning iterator

end  - returns the ending iterator

r - in the reverse order

c - returns a const iterator

1. forward_list没有reverse order因为它是单向的,但是有多余的before_begin/before_cbegin来方便在list首端insert。

2. unordered associative containers (unordered_set, unordered_map, unordered_multiset, unordered_multimap)没有reverse因为它是unordered所以单向遍历已经足够了。

4. container adaptors (stack, queue, priority_queue)没有iterators所以没有办法在不影响容器内元素的情况下遍历。

Capacity

size, empty, max_size

size - returns the size

empty - check if the size is zero

max_size - the max size the container can hold

1. forward_list doesn't have size becuase it saves space (stackoverflow)

2. container adaptors(stack, queue, priority_queue) don't have max_size.

3. vector & deque have resize and shrink_to_fit.

4. vector has capacity and reserve. (size vs max_size vs capacity)

Element Access

front, back: sequence container (array, vector, deque, forward_list, list) and queue

                    * forward_list没有back因为它是单向的

top: stack and priority_queue

[], at: array, vector, deque, map and unordered_map

                   * the others we use iterators through find

data: array, vector (contiguous)

Modifier

swap: all

insert, erase, clear: except for container adaptors (stack, queue, priority_queue)

                     *forward_list use insert_after and erase_after

assignsequence container without array (vector, deque, forward_list, list)

push_back, pop_back: vector, deque, list

push_front, pop_front: forward_list, deque, list

push, popcontainer adaptors (stack, queue, priority_queue)

Operations

splice, remove, remove_if, unique, merge, sort, reverse: forward_list and list

                 * forward_list use splice_after

find, count, equal_range: all except for sequence containers

                 * sequence containers use find from algorithm

lower_bound, upper_bound: associative containers (set, map, multiset, multimap)

Sequence Containers: array, vector, deque, forward_list, list

Associative Containers: set, map, multiset, multimap

Unordered Associative Containers: unordered_set, unordered_map, unordered_multiset, unordered_multimap

Container Adaptors: stack, queue, priority_queue

Special Attention to containers with order: set, map, multiset, multimap, priority_queue

How to write the comparator for them and the sort <algorithm>

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

struct Node{
    int val;
    Node* next;
    Node(int x): val(x), next(NULL) {};
};

struct cmp{
    bool operator() (Node* a, Node* b){
        return a->val > b->val;
    }
};

int main()
{
    // ordered container
    priority_queue<Node*> q1;
    for(int i=0; i<10; i++){
        q1.push(new Node(i));
    }
    cout<<"q1:";
    while(!q1.empty()){
        int t = q1.top()->val; q1.pop();
        cout<<" "<<t;
    }
    cout<<endl;
    
    // use the comparator to place the smallest to the top
    priority_queue<Node*, vector<Node*>, cmp> q2;
    for(int i=0;i<10; i++){
        q2.push(new Node(i));
    }
    cout<<"q2:";
    while(!q2.empty()){
        int t = q2.top()->val; q2.pop();
        cout<<" "<<t;
    }
    cout<<endl;
    
    
    // unordered container
    vector<Node*> vec{new Node(1), new Node(4), new Node(3), new Node(6)};
    sort(vec.begin(), vec.end(), [](Node* a, Node* b){
        return a->val < b->val;
    });
    cout<<"vec:";
    for(Node* a: vec) cout<<" "<<a->val;
    cout<<endl;
    
    return 0;
}

run the above code here

The excel file records all the differences.

猜你喜欢

转载自blog.csdn.net/real_lisa/article/details/83435890
今日推荐