use of C + + STL basic containersCategory: C/c++/vc2014-04-09 21:01 583 People read comments (0) favorite reports Stllistmapdequeset
There are two types of containers in C + +: Sequential containers and associative containers. The sequential containers are mainly vector, list, deque and so on. Where vectors represent a contiguous memory, based on an array implementation, List represents noncontiguous memory, is based on a linked list, deque is similar to vectors, but provides bidirectional support for insertions and deletions for the first element. The associated containers are mainly map and set. Map is a key-value form and set is a single value. Map and set can only hold unique key,multimap and multiset to hold multiple identical keys.
The container class automatically requests and frees memory, so new and delete operations are not required.
First, Vector
Vectors are based on a template and need to contain the header file vector.
1. Defining and initializing
1. Defining and initializing Vector<int> VEC1; Default initialization, VEC1 is empty Vector<int> vec2 (VEC1); Initialize VEC2 with VEC1 Vector<int> vec3 (Vec1.begin (), Vec1.end ());//use VEC1 to initialize VEC2 Vector<int> VEC4 (10); 10 Elements of a value Vector<int> vec5 (10,4); 10 Elements of a value 2. Common methods of operation Vec1.push_back (100); adding elements int size = Vec1.size (); Number of elements BOOL IsEmpty = Vec1.empty (); Determines whether the empty cout<<vec1[0]<<endl; Get the first element Vec1.insert (Vec1.end (), 5, 3); Inserts a value element from the Vec1.back position Vec1.pop_back (); Delete End Element Vec1.erase (Vec1.begin (), Vec1.end ());//delete elements between, other elements move forward cout<< (VEC1==VEC2) True:false; Judge whether equal = =,! =, >=, <= ... Vector<int>::iterator iter = Vec1.begin (); Get Iterator Header Address Vector<int>::const_iterator c_iter = Vec1.begin (); Get const Type iterator Vec1.clear (); Empty elements 3. Traverse Subscript method int length = Vec1.size (); for (int i=0;i<length;i++) { cout<<vec1[i]; } cout<<endl<<endl; Iterator method Vector<int>::const_iterator iterator = Vec1.begin (); for (; Iterator! = Vec1.end (); iterator++) { cout<<*iterator; } |
Second, List
List is a bidirectional linked list implemented by STL, which allows fast insertion and deletion compared to vectors (vectors), but random access is slower. Need to add header file list
1. Defining and initializing List<int> Lst1; Create an empty list List<int> Lst2 (3); Create a list with three elements List<int> Lst3 (3,2); Create a list with three elements List<int> Lst4 (LST2); Initialize LST4 with Lst2 List<int> Lst5 (Lst2.begin (), Lst2.end ()); With Lst4 2. Common methods of operation Lst1.assign (Lst2.begin (), Lst2.end ()); assigning values Lst1.push_back (10); Add Value Lst1.pop_back (); Delete End Value Lst1.begin (); Iterator that returns the first value Lst1.end (); Iterator that returns the tail value Lst1.clear (); Empty value BOOL isEmpty1 = Lst1.empty (); Judged to be empty Lst1.erase (Lst1.begin (), Lst1.end ()); Delete Element Lst1.front (); Returns a reference to the first element Lst1.back (); Returns a reference to the last element Lst1.insert (Lst1.begin (), 3,2); Inserts from a specified location Lst1.rbegin (); Returns a forward pointer to the first element Lst1.remove (2); Delete all the same elements Lst1.reverse (); Reverse Lst1.size (); Contains the number of elements Lst1.sort (); Sort Lst1.unique (); Delete adjacent repeating elements 3. Traverse Iterator method for (List<int>::const_iterator iter = Lst1.begin (); ITER! = Lst1.end (); iter++) { cout<<*iter; } cout<<endl; |
Third, deque
Deque container classes are similar to vectors, support random access and quick insert deletions, and it takes linear time to operate on a location in a container. Unlike vectors, deque also supports inserting data from the beginning: Push_front (). The remainder is similar to the use of vector manipulation methods.
Iv. Map
The map container in C + + provides a key-value pair (Key/value) container, and the difference between map and Multimap is only that multiple allows a key to correspond to multiple values. You need to include a header file map. For iterators, you can modify the real value without modifying the key. Map is automatically sorted by key.
1. Defining and initializing Map<int,string> Map1; Empty map 2. Common methods of operation MAP1[3] = "Saniya"; adding elements Map1.insert (Map<int,string>::value_type (2, "Diyabi"));//Insert Element Map1.insert (pair<int,string> (1, "Siqinsini")); Map1.insert (make_pair<int,string> (4, "V5")); String str = map1[3]; Value,key cannot be modified according to key Map<int,string>::iterator Iter_map = Map1.begin ();//Get Iterator header address int key = iter_map->first; Get Eky String value = iter_map->second; Get value Map1.erase (ITER_MAP); Delete iterator data Map1.erase (3); Delete value based on key Map1.size (); Number of elements Map1.empty (); Judge Null Map1.clear (); Clear all elements 3. Traverse for (Map<int,string>::iterator iter = Map1.begin (); Iter!=map1.end (); iter++) { int Keyk = iter->first; String Valuev = iter->second; } |
Five, set
The meaning of set is a set, it is an ordered container, the elements inside are sorted, support inserting, deleting, finding and so on, just like a set. All the operations are done strictly within LOGN time, and the efficiency is very high. The difference between set and Multiset is that set inserts cannot be the same element, but Multiset can be the same. Set default Auto sort. Use a method similar to list.
Vi. Summary of various containers (transfer from: HTTP://HI.BAIDU.COM/EWOOK/ITEM/514FC22ECDE5940E73863E65)
(1) vector
Internal data structure: array.
Random access to each element, the time required is constant.
The time required to add or delete elements at the end is independent of the number of elements, and the time required to add or delete elements in the middle or at the beginning varies linearly with the number of elements.
Elements can be dynamically added or reduced, memory management is done automatically, but programmers can use the reserve () member function to manage memory.
The vector's iterator is invalidated when the memory is reassigned (the element it points to is no longer the same as before or after the operation). When more than capacity ()-size () elements are inserted into the vector, memory is reassigned, all iterators are invalidated, otherwise iterators that point to any element after the current element are invalidated. When an element is deleted, an iterator that points to any element after the deleted element is invalidated.
(2) deque
Internal data structure: array.
Random access to each element, the time required is constant.
The time required to add elements at the beginning and end is independent of the number of elements, and the time required to add or delete elements in the middle varies linearly with the number of elements.
Elements can be dynamically increased or decreased, memory management is done automatically, and no member functions are provided for memory management.
Adding any element will invalidate the deque iterator. Deleting an element in the middle of a deque invalidates the iterator. When you delete an element at the head or tail of a deque, only the iterator that points to that element is invalidated.
(3) List
Internal data structure: Two-way loop linked list.
An element cannot be accessed randomly.
Bidirectional traversal is possible.
The time required to add or remove elements at the beginning, end, and anywhere in the middle is constant.
Elements can be dynamically added or reduced, and memory management is done automatically.
Adding any element does not invalidate the iterator. When you delete an element, other iterators are not invalidated except for the iterator that points to the currently deleted element.
(4) slist
Internal data structure: one-way linked list.
Cannot traverse in two directions, only from front to back.
Other features are similar to list.
(5) Stack
Adapter, which converts any type of sequence container to a stack, typically using deque as a supported sequence container.
The element can only last in, first out (LIFO).
You cannot traverse the entire stack.
(6) Queue
Adapter, which converts any type of sequence container into a queue, typically using deque as a supported sequence container.
The element can only be first in and out (FIFO).
You cannot traverse the entire queue.
(7) priority_queue
Adapter, which converts any type of sequence container into a priority queue, typically using vectors as the underlying storage method.
Only the first element can be accessed and the entire priority_queue cannot be traversed.
The first element is always the highest-priority element.
(8) Set
Keys and values are equal.
Key is unique.
The elements are sorted in ascending order by default.
If the element that the iterator points to is deleted, the iterator is invalidated. Any other operation that adds or removes an element does not invalidate the iterator.
(9) Multiset
Keys can be not unique.
Other features are the same as set.
(10) Hash_set
In comparison with set, the elements in it are not necessarily ordered, but are assigned according to the hash function used, which provides a faster search speed (of course, it is related to the hash function).
Other features are the same as set.
(11) Hash_multiset
Keys can be not unique.
Other features are the same as hash_set.
(12) Map
Key is unique.
The ascending order of the element default keys.
If the element that the iterator points to is deleted, the iterator is invalidated. Any other operation that adds or removes an element does not invalidate the iterator.
(13) Multimap
Keys can be not unique.
Other features are the same as map.
(14) Hash_map
In contrast to map, the elements in it are not necessarily sorted by key values, but are dispatched according to the hash function used, which provides a faster search speed (as well as the hash function).
Other features are the same as map.
(15) Hash_multimap
Keys can be not unique.
Other features are the same as hash_map.
Use of C + + STL basic containers