Container
Vector
How to store
-
vector<int> v;
: initialize a empty vector -
vector<int> v(init_size, init_value);
-
vector<int> v(v1)
: copy another vector -
vector<int> v(v1.cbegin(), v1.cbegin()+5)
: copy five elements in vectorv1
-
vector<int> v(22, 33, 55)
: there are three elements in it -
vector<int> v={22, 33, 55}
: list initialization -
v.insert(v.begin(), 8);
,v.insert(v.begin()+2, 1);
:v.insert(pos, num, value1, value2,...)
-
v.push_back(8);
-
v.assign(nums, val)
:nums
is the number of elements,val
is the value of initialization -
v.assign({22, 33, 55});
: change the value in vector -
v.assign(arr, arr+5);
: use array as the parameter -
v.aasign(v1.begin(),v1.begin()+5);
: use iterator as the parameter
How to access
-
v[i]
: array access method -
iterator:
v.begin()
,v.end()
-
v.at(i)
,v.front()
,v.back()
: return the value directly -
v.empty();
: check whether the vector is empty -
v.size()
: return the size of current vector -
v.capacity()
: return the space allocated to the vector
How to delete
-
v.erase(v.begin())
: delete the first element -
v.erase(v.begin(), v.begin()+3)
: delete the [1st, 3rd) elements -
v.pop_back();
-
v.clear()
: delete all the elements in vector
Deque
- dynamic array, similar with vector, so there are some tips only
v.push_back()
,v.pop_back()
v.push_front()
,v.pop_front()
- you can store the element in the beginning or end of the deque, this is also the difference with vector
List
How to store
list<int> l;
,list<int> l(10):
,list<int> l(10, 99);
list<int> l(l1);
,list<int> l(v.cbegin(), v.cend());
l.push_back(5);
,l.push_front(5);
l.insert(l.begin(), 2);
,l.insert(l.begin(), 4, 0);
,l.insert(l.begin(), l1.begin(), l1.end());
How to delete
l.erase(l.begin());
,l.erase(l.begin(), l.end());
l.clear();
l.remove(2)
: delete the element whose value is 2
Others
std::sort();
,std::reverse();
forward_list
is unidirectional linked list, onlypush_front(5);
- 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
set<int> s;
,set<int, sort<int>> s;
: define the sort() functionmultiset<int> mest(s1.cbegin(), set1.cend());
set1.insert(1);
,mset.insert(s.begin(), s.end());
: insert a segment of elements
2 How to access / search
mset.count(-1);
: count the number of the elements with value ‘-1’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.s.size()
: return the size of the set container
3 How to delete
s1.erase(key);
s1.erase(element);
: delete the iterators1.erase(e1,e2);
: delete the segment of [e1, e2) iteratorss1.clear();
4 unordered_set & unordered_multiset
- each element in it is allocated a position using
HashFunction(key, tablesize)
which is related with key, insertion order and number of bucket uset.max_bucket_count()
: the number of bucketuset.max_load_factor()
: the maximum load factoruset.load_factor()
: the average load factor
Map & multiset
map is a kind of key-value container for searching elements
1 How to store
map<int, string> m;
map<int, string> m(m1);
, ```map<int, string> m(m1.cbegin(), m1,cend());`: copy another mapmap<int, string, Sort<int>> m(m1.cbegin(), m1.cend());
: the Sort is astruct
in which there is abool operator ()
function to be used as sort functionm.insert(make_pair(-1, "Minus one"));
m.insert(pair<int, string>(1000, "One Thousand"));
m.insert(map<int, string>::value_type (3, "Three"));
m[1001]="One Thousand and One";
2 How to access / search
m.count(key);
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
m.erase(key);
: return the number of elements deletedm.erase(element);
: delete the iteratorm.erase(m.lower_bound(1000), m.upper_bound(1000));
: delete a segment of elements with key value ‘1000’
note:
struct
is similar withclass
, but the members are defined to be public, so we can use astruct
to define the sort function for map or set where there is abool operator () ()
function to be used- the
unordered_map
andunordered_multimap
is similar withunordered_set
andunordered_multiset
, using the hash function to position each element