Learn more C ++ primer (9)-ordered container

Source: Internet
Author: User

Ordered container:
Vector: supports fast random access;
List: supports fast insert/delete, similar to linked list
Deque: Refreshing queue.
Use the default constructor to achieve the best runtime performance and make containers easier to use.

Initialize a container as a copy of another container:
Vector <int> ivec;
Vecror <int> ivec2 (ivec );
The container type and element type must be the same.

The constructor that accepts the container size as a parameter only applies to ordered containers, but the associated containers do not support such initialization.

The container element type must meet the following two constraints:
1) Value assignment is supported for element types;
2) an object of the element type must be able to be copied.

When writing a program using the iterator, you must note which operations will invalidate the iterator (such as adding and deleting elements ).
Using an invalid iterator will cause serious running errors. When writing a loop, insert the element to the vector or deque container.
The program must ensure that the iterator is updated after each loop.

Reverse_iterator: The Reverse iterator, an iterator that is addressing in reverse order, traverses the container from the back to the front, and reverses some
Related iterator operations.

Container relational OPERATOR: similar to the relational operation of strings, and only two containers are allowed to perform relational operations defined by their element types.

The assign operation of the container: assign a new value to the container. The operation first deletes all elements originally stored in the container. There are two versions.
Container swap operation: no elements are deleted or inserted, and exchange is implemented within the constant time.
Example:
# Include <iostream>
# Include <list>
Using namespace STD;

Int main ()
{
List <int> first;
List <int> second;

First. Assign (7,100); // 7 ints with value 100

Second. Assign (first. Begin (), first. End (); // a copy of first

Int myints [] = {1776,7, 4 };
First. Assign (myints, myints + 3); // assigning from Array

Cout <"size of first:" <int (first. Size () <Endl;
Cout <"size of second:" <int (second. Size () <Endl;
Return 0;
}

Output:
Size of first: 3
Size of second: 7

Every time a vector container has to allocate a new bucket, it is re-allocated by the allocation policy that doubles the current capacity.
Continuously store elements at minimal cost. The convenience of access elements makes up for the storage cost.

Container features and selection:
Considerations for selecting a container:
1) cost of adding or deleting elements in the middle of the container;
2) The cost of Random Access to the elements of the execution container.

Vector and deque: Advantages: Fast random access; disadvantages: high cost of inserting or deleting elements at any location, similar to arrays.
List: advantage: Fast insertion and deletion at any location; disadvantage: Slow random access, similar to linked list;
A difference between deque and vector: deque provides efficient insert and erase operations in its header, just like at the end of the container.

Inserting elements at the beginning or end of the deque container does not invalidate any iterator.

Since each access to a vector container is a fixed offset from its starting point, its random access is very efficient. In the list container, the unique element that moves between elements
The method is to follow the pointer sequentially.

Container selection principles:
1) if the program requires Random Access to elements, select deque or vectore;
2) If the program must insert or delete elements in the middle of the container, the list container should be used;
3) if the program is not in the middle of the container, but inserts or deletes elements in the header or tail of the container, deque is used;
4) if you only need to insert an element in the middle of the container when reading the input and then randomly access the element, you can consider reading the element into
List container, then sort the containers to make them suitable for sequential access, and then copy the sorted list container to a vector.
In short, decide which container to use may need to analyze the performance of various types of operations required by the application for various container types.

Priority-queue: priority queue. If an element is inserted, it is placed before an element with a lower priority.

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.