Sequential containers:
- Vector Quick Insert and delete from behind, direct access to any element ( random access )
- Deque Quick Insert and delete from the front or back, direct access to any element ( random access )
- List doubly linked list, inserting and deleting from anywhere
Associative containers:
- Set Quick Find, no duplicate values are allowed.
- Multiset Quick Find, allow duplicate values.
- Map one-to-many mappings, fast lookup based on keywords, no duplicate values are allowed.
- Multimap one-to-many mappings, fast lookup based on keywords, allowing duplicate values.
Container adapter:
- Stack LIFO (Stack)
- Queue FIFO (queuing)
- Priority queue Highest Precedence first dequeue
Vectors (Vector container)
A linear sequential structure, equivalent to a dynamic array.
After creation, the memory is automatically allocated a contiguous space for storage, and the size can be pre-specified (capacity () returns this size). When the size exceeds the pre-allocated space, it will re-request a piece of memory, but this allocation is time-consuming ( so the pre-consideration of the required size can save time ), the reallocation of space will do the following:
- First, request a larger piece of memory
- The original data is then copied to the new block of memory.
- Second, destroy the object in the original memory block. (destruction)
Finally, the original space is released.
So, if the original amount of data is large, such operations can lead to poor performance. Although it is a dynamic array, knowing the size of the data beforehand is the best way to play the performance of the vector.
Characteristics:
- Dynamic expansion
- Random Access
- Space-saving (continuous storage, large fragments less)
- Inefficient internal insertion and deletion (sequential table)
- Only at the tail push,pop
- Pre-allocating the right size to optimize performance
constructor function
vector<int> v1 //一个空的vectorvector<int> v2(5,1)//大小为5值为1的vector
To assign or compare
==, !=, <=, >=, <, >访问某个特定位置 => []如果两个vector相等: -则两个vector容量相等 -相同位置的元素相等比较按照词典规则
assign () Assigning a value to an element
//将[start,end)的元素复制到当前vectorvoid assign(input_iterator start,input_iterator end);//赋num个值为value的元素到vector中,会清除掉vector之前的内容void assign(size_type num, const type &val)
At () returns the specified position element
//和v[i]差不多,但是比v[i]安全TYPE at(size_type loc);
Back () returns the last element
Begin () returns an iterator to the first element
Clear () Clears all elements
Empty () to determine if NULL
End () returns an iterator to the last element (pointing to the next position of the last element)
Erase () deletes the specified element
//删除loc处元素erase(iterator loc);//删除start和end之间的元素erase(iterator start,iterator end);
Front (); Returns a reference to the first element
Get_allocator (); Returns the vector's memory allocator
Insert () Inserts an element
//在loc前插入val,返回这个元素的迭代器insert(iterator loc ,const TYPE &val);//在loc前插入Num个值为val的元素insert(iterator loc,size_type num,const TYPE &val);//在loc前插入[start,end)内所有元素insert(iterator loc,iterator start,iterator end);
Max_size () returns the maximum number of elements to hold
Pop_back () removes the last element
Push_back () Inserts an element at the end
Rbegin () tail inverse iterator
Rend () head inverse iterator
Reserve () Set the minimum space (similar to initialization). )
Resize () change size
Size () returns sizes
Swap () Exchange vector
C + + Standard Template Library _note (1)