C++容器及使用

Vector

How to store

  1. vector<int> v;: initialize a empty vector

  2. vector<int> v(init_size, init_value);

  3. vector<int> v(v1): copy another vector

  4. vector<int> v(v1.cbegin(), v1.cbegin()+5): copy five elements in vector v1

  5. vector<int> v(22, 33, 55): there are three elements in it

  6. vector<int> v={22, 33, 55}: list initialization

  7. v.insert(v.begin(), 8);, v.insert(v.begin()+2, 1);: v.insert(pos, num, value1, value2,...)

  8. v.push_back(8);

  9. v.assign(nums, val): nums is the number of elements, val is the value of initialization

  10. v.assign({22, 33, 55});: change the value in vector

  11. v.assign(arr, arr+5);: use array as the parameter

  12. v.aasign(v1.begin(),v1.begin()+5);: use iterator as the parameter

How to access

  1. v[i]: array access method

  2. iterator: v.begin(), v.end()

  3. v.at(i), v.front(), v.back(): return the value directly

  4. v.empty();: check whether the vector is empty

  5. v.size(): return the size of current vector

  6. v.capacity(): return the space allocated to the vector

How to delete

  1. v.erase(v.begin()): delete the first element

  2. v.erase(v.begin(), v.begin()+3): delete the [1st, 3rd) elements

  3. v.pop_back();

  4. v.clear(): delete all the elements in vector

Deque

  1. dynamic array, similar with vector, so there are some tips only
  2. v.push_back(), v.pop_back()
  3. v.push_front(), v.pop_front()
  4. you can store the element in the beginning or end of the deque, this is also the difference with vector

List

How to store

  1. list<int> l;, list<int> l(10):, list<int> l(10, 99);
  2. list<int> l(l1);, list<int> l(v.cbegin(), v.cend());
  3. l.push_back(5);, l.push_front(5);
  4. l.insert(l.begin(), 2);, l.insert(l.begin(), 4, 0);, l.insert(l.begin(), l1.begin(), l1.end());

How to delete

  1. l.erase(l.begin());, l.erase(l.begin(), l.end());
  2. l.clear();
  3. l.remove(2): delete the element whose value is 2

Others

  1. std::sort();, std::reverse();
  2. forward_list is unidirectional linked list, only push_front(5);
  3. the list is node link structure, so you won’t worry it when delete a element from middle.

Set & multiset

set is a kind of container used for searching one-dimensional elements.

1 How to store

  1. set<int> s;, set<int, sort<int>> s;: define the sort() function
  2. multiset<int> mest(s1.cbegin(), set1.cend());
  3. set1.insert(1);, mset.insert(s.begin(), s.end());: insert a segment of elements

2 How to access / search

  1. mset.count(-1);: count the number of the elements with value ‘-1’
  2. e=s.find(-1);: return the iterator with value ‘-1’, if there are lots of elements with the value, the iterator found is the last one, so you can move forward count()-1 times to traverse all of them.
  3. s.size(): return the size of the set container

3 How to delete

  1. s1.erase(key);
  2. s1.erase(element);: delete the iterator
  3. s1.erase(e1,e2);: delete the segment of [e1, e2) iterators
  4. s1.clear();

4 unordered_set & unordered_multiset

  1. each element in it is allocated a position using HashFunction(key, tablesize) which is related with key, insertion order and number of bucket
  2. uset.max_bucket_count(): the number of bucket
  3. uset.max_load_factor(): the maximum load factor
  4. uset.load_factor(): the average load factor

Map & multiset

map is a kind of key-value container for searching elements

1 How to store

  1. map<int, string> m;
  2. map<int, string> m(m1);, ```map<int, string> m(m1.cbegin(), m1,cend());`: copy another map
  3. map<int, string, Sort<int>> m(m1.cbegin(), m1.cend());: the Sort is a struct in which there is a bool operator () function to be used as sort function
  4. m.insert(make_pair(-1, "Minus one"));
  5. m.insert(pair<int, string>(1000, "One Thousand"));
  6. m.insert(map<int, string>::value_type (3, "Three"));
  7. m[1001]="One Thousand and One";

2 How to access / search

  1. m.count(key);
  2. m.find(key);: return a iterator with the specific key value, move backward to access all the element with the key value if there are more than one answer

3 How to delete

  1. m.erase(key);: return the number of elements deleted
  2. m.erase(element);: delete the iterator
  3. m.erase(m.lower_bound(1000), m.upper_bound(1000));: delete a segment of elements with key value ‘1000’

note:

  • struct is similar with class, but the members are defined to be public, so we can use a struct to define the sort function for map or set where there is a bool operator () () function to be used
  • the unordered_map and unordered_multimap is similar with unordered_set and unordered_multiset, using the hash function to position each element

Stack

Queue

猜你喜欢

转载自blog.csdn.net/liyingjiehh/article/details/85249026