STL 4: STL container: select the time, delete the element, and the iterator fails.

Source: Internet
Author: User

I. Category:
  • StandardSTLSequential container: Vector, String, deque, and list.
  • StandardSTLAssociated container: Set, Multiset, map, and multimap.
  • Non-standard sequence containerSlist and rope. Slist is a one-way linked list, and rope is essentially a heavy string
  • Non-standard associated containerHash_set, hash_multiset, hash_map, and hash_multimap.
  • Several TypesStandard non-STLContainerIncluding array, bitset, valarray, stack, queue, and priority_queue
It is worth noting that arrays can work with STL algorithms because pointers can be used as arrays as iterators. Ii. Delete Elements

If you want to delete something, remember to add the so-called erase deletion algorithm after removing the algorithm. In the end, you still need to call the member function to delete an element, but because remove does not know which container it acts on, therefore, the remove algorithm cannot really delete an element. vector vector <int> V; V. reserve (10); For (INT I = 1; I <= 10; ++ I) {v. push_back (I);} cout <v. size (); // 10 V [3] = V [5] = V [9] = 99; remove (v. begin (), V. end (), 99); // v. erase (remove (v. begin (), V. end (), 99), V. end (); cout <v. size (); // 10! 2. listlist <int> listtest; listtest. Remove (99); // This member function will actually Delete elements, which is much more efficient than erase + remove and remove_if. However, unique behavior is also like remove. It is used to delete items (adjacent duplicate values) from a range without accessing the container holding the range element. If you really want to delete elements from the container, you must call unique and erase in pairs. Unique is similar to remove in the list. Just like list: Remove actually deletes (and is much more efficient than erase-Remove ). List: Unique also deletes adjacent duplicate values (also more efficient than erase-unique ).

Three iterators failed:

 

A question raised by a netizen:

Void main ()
{
Vector <string> VCs;
VCs. push_back ("this is ");
Vector <string >:: iterator it = VCs. Begin ();
Int I = 9;
For (; it! = VCs. End (); ++ it)
{
Cout <"caplity of vector is:" <VCs. Size () <Endl;

Cout <"--->" <* It <Endl; // if you remove this sentence, a value that exceeds the Vector
// The cycle of the size. Can you explain why?
If (I = 9)
{
VCs. push_back ("this is bbbbb ");
Cout <"VCs. Push! "<Endl;
}
I = 8;
}
} Typical iterator failure ....

Vector:
1. When an element is inserted (push_back), The iterator returned by the end operation is definitely invalid.
2. When an element is inserted (push_back), the return value of capacity is different from that before the element is inserted. Then, the entire container needs to be reloaded, And the iterator returned by the first and end operations will become invalid.
3. After the delete operation (erase, pop_back) is performed, all the iterators pointing to the delete vertex are invalid. All the iterators pointing to the elements behind the delete vertex are also invalid. Deque iterator failure:
1. inserting elements at the front or end of the deque container does not invalidate any iterator.
2. Deleting an element at its header or tail will only invalidate the iterator pointing to the deleted element.
3. The insert and delete operations at any other location of the deque container will invalidate all iterators pointing to the container element. LIST/set/map1. When deleting a node, the list pointing to the node to which the iterator fails <int> intlist;
List <int>: iterator it = intlist. Begin ();
While (it! = Intlist. End ())
{
It = intlist. Erase (it );
......
} 4. Select the time <> -- summarize the characteristics of various containers (1) vector
Internal data structure: array.
Each element is randomly accessed. The time required is a constant.
The time required to add or delete an element at the end is irrelevant to the number of elements. The time required to add or delete an element at the beginning or in the middle changes linearly with the number of elements.
You can dynamically add or remove elements and manage the memory automatically. However, you can use the reserve () member function to manage the memory.
The iterator of the vector will become invalid when the memory is re-allocated (the elements it points to are no longer the same before and after the operation ). When more than capacity ()-size () elements are inserted into the vector, the memory will be re-allocated and all iterators will become invalid; otherwise, the iterator pointing to any element after the current element fails. When an element is deleted, the iterator pointing to any element after the element is deleted becomes invalid.

(2) deque
Internal data structure: array.
Each element is randomly accessed. The time required is a constant.
The time required to add an element at the beginning and end is irrelevant to the number of elements. The time required to add or delete an element in the middle changes linearly with the number of elements.
Elements can be dynamically added or removed, and memory management is completed automatically. member functions used for memory management are not provided.
Adding any element will invalidate the deque iterator. Deleting an element in the middle of deque will invalidate the iterator. When a deque header or tail deletes an element, only the iterator pointing to the element fails.

(3) List
Internal data structure: Bidirectional Ring linked list.
You cannot randomly access an element.
Bidirectional traversal is supported.
The time required to add or delete an element at the beginning, end, or in the middle is constant.
You can dynamically add or remove elements and manage the memory automatically.
Adding any element will not invalidate the iterator. When an element is deleted, other iterators will not expire except the iterator pointing to the currently deleted element.

(4) slist
Internal data structure: one-way linked list.
It cannot be traversed in two directions. It can only be traversed from front to back.
Other features are similar to list.

(5) Stack
Adapter, which can convert any type of sequence container into a stack. Generally, deque is used as the supported sequence container.
The element can only be post-in, first-out (LIFO ).
The entire stack cannot be traversed.

(6) queue
It can convert any type of sequence container into a queue. Generally, deque is used as the supported sequence container.
The element can only be FIFO ).
The entire queue cannot be traversed.

(7) priority_queue
It can convert any type of sequence container into a priority queue. Generally, vector is used as the underlying storage mode.
Only the first element can be accessed, and the whole priority_queue cannot be traversed.
The first element is always the element with the highest priority.

(8) Set
The keys and values are equal.
The key is unique.
Elements are arranged in ascending order by default.
If the element to which the iterator points is deleted, the iterator becomes invalid. Any other operations to add or delete elements will not invalidate the iterator.

(9) Multiset
The key may not be unique.
Other features are the same as those of set.

(10) hash_set
Compared with set, the elements in it are not necessarily sorted, but allocated by the hash function used. It can provide faster search speed (of course, related to the hash function ).
Other features are the same as those of set.

(11) hash_multiset
The key may not be unique.
Other features are the same as hash_set.

(12) Map
The key is unique.
The elements are sorted in ascending order by default.
If the element to which the iterator points is deleted, the iterator becomes invalid. Any other operations to add or delete elements will not invalidate the iterator.

(13) multimap
The key may not be unique.
Other features are the same as those of map.

(14) hash_map
Compared with map, the elements in it are not necessarily sorted by key values, but are allocated according to the hash function used, it provides a faster search speed (of course, also related to the hash function ).
Other features are the same as those of map.

(15) hash_multimap
The key may not be unique.
Other features are the same as hash_map.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.