C + + sequential container Knowledge Summary

Source: Internet
Author: User

A container is a collection of objects that hold a particular type. C + + containers can be divided into two categories: sequential containers and associative containers. The order container's element arrangement is independent of the element value size, but is determined by the order in which the elements are added to the container. The standard library defines the three types of sequential containers: vector, List, and Deque (double-ended queues). In addition, the standard library provides three types of container adapters: Stack, queue, and Prioroty_queue. The adapter is based on the operation provided by the original container type, and is adapted to the underlying container type by defining a new operating interface. See the table below

sequential container vectors                    support fast random access list                      support Quick Insert / Delete deque                     double-ended queue sequential container adapter stack LIFO in-                     first-out (LIFO) stack queue                     FIFO (FIFO) queue Priority_queue            Priority-managed queues
I. Definition and initialization

Before you define an object of a container type, you must include the associated header file. As follows:

#include <list>#include<vector>#include<deque>

All containers have a default constructor defined, and a default constructor can be used to initialize an empty container object, as follows:

list<int>    IList;

In addition, the container has several constructors that can be used to initialize the container object, as follows:

C<t>    C         creates an empty container named C. C is the container name, and T is the element type. Applies to all containers C    C (C2)        to create a copy of the container C2 c. Both must be containers and elements of the same type. Applies to all containers C    C (b,e)       to create C, whose elements are copies of the elements in the range of iterator B and e tags. For all containers C    C (n,t)       creates C with an element of n values T, where t must be a value of the element type of the container type C or a value that can be converted to that type. Container C that only applies to order container C    C (n)         to create n initialization elements. Applies only to sequential containers

It is worth noting that the constructors that accept the size of the container are only suitable for sequential containers and not for associative containers. Since the pointer is an iterator, we can also initialize the container by using a pair of pointers in the built-in array:

 char  *words[]={ " hi  , "   How  , "   is  , "   You "};  size_t words_size=sizeof  (words)/sizeof     (char  *);  //  initialize words  List<string  > Words2 (words,words+words_size); 

As we said earlier, a container is a collection of elements of a type that is stored, so you can define that the element is a container of type container. For example, you can define a vector type of container Ivec, whose element is a string-type vector. Note, however, that the following spaces must be used when specifying container elements as container types:

vector< vector<string> >    ivec;     // Legal. There are spaces between two > vector<vector<string>>    Ivec;       // error. There is no space between two >,>> will result in ambiguity
Two. Common operation

Sequential containers have built-in useful operations: (1) Adding elements to the container, (2) deleting elements in the container, (3) Setting the container size, (4) (if any) getting the first and last elements within the container.

1.begin and End

The Begin and end operations produce iterators that point to the next position of the first and last elements within a container. There are also reverse iterators (reverse iterators that traverse the container from behind and reverse some of the related iterator operations) Rbegin and Rend. As follows:

C.begin ()            returns an iterator that points to the first element of container C c.end ()              returns an iterator that points to the next position of the last element of container C C.rbegin ()           returns an inverse iterator. It points to the last element of container C c.rend ()             returns an inverse iterator that points to the previous position of the first element of container C
2. Container add element operation

The following table is an operation that adds elements to the order table. Note the scope and return type of the application.

C.push_back ()            adds an element with a value of t at the tail of container C. return void type C.push_front (t)          adds an element with a value of t at the front end of container c. Returns the void type. applies only to the list and Deque container type c.insert (p,t)            inserts a new element with a value of t before the element that the iterator p points to. Returns an iterator that points to the newly added element C.insert (p,n,t)          inserts a new element with n values of T before the element that the iterator p points to. The return void type C.insert (p,b,e) inserts an element in the          range labeled by Iterators B and E before the element that the iterator p points to. return void type

The Push_front only applies to the list and Deque container types, and this operation implements the ability to insert new elements in the header.

// Add 0,1,2,3 list<int> IList at the end of the container with Push_front    ;  for (size_t i=0; i!=4; + +i)    ilist.push_front (i); // the sequence of elements in IList is: 3,2,1,0

It is important to note that any insert or push operation can cause an iterator to fail. So when writing loops to insert elements into vectors and deque containers, the program must ensure that the iterator is updated after each loop.

vector<string>Ivec;vector<string>::iterator iter=Ivec.begin (); while(cin>>word) ITER=ivec.insert (Iter,word);//the same effect as Push_back//The following is wrong, as the ITER has failed at this timevector<string>Ivec;vector<string>::iterator iter=Ivec.begin (); while(cin>>word) Ivec.insert (Iter,word); //error, after a round of insert, ITER is invalidated and cannot be called again
3. Operation of container size

All containers provide the following operations related to container size. Note The resize operation may invalidate the iterator.

c.size ()                returns the number of elements in container C. The return type is c::size_typec.max_size ()            returns the maximum number of elements that container C can hold. Returns a               Boolean value that returns a container size of 0 for a c::size_typec.empty () c.resize (n)             adjusts the length of container C so that it can hold n elements. If n<c.size (), remove the extra element c.resize (n,t) to           resize the container C so that it can hold n elements. All newly added element values are T

Doing a resize operation on a vector and deque container may invalidate all of its iterators. For all container types, iterators that point to deleted elements are invalidated if the resize operation compresses the container.

4. Operations to access container elements

The following is an operation that accesses a container element. Note • Using an out-of-bounds subscript, or calling the front or back function of an empty container, can cause serious errors in the program.

c.back ()                returns a reference to the last element of container C. If C is empty, the operation does not define C.front () to               return a reference to the first element of container C. If c is null, the operation does not define C[N]                    returns a reference to the element labeled N. If n<0 or n>=c.size (), the operation is undefined. Applies only to vectors and deque containers c.at (n)                 returns a reference to the element labeled N. If the subscript is out of bounds, the operation is undefined. For vector and deque containers only
5. Removing container element operations

The following is an action to delete a container element. The delete operation invalidates some iterators and requires special attention. The first operation in the following table before you delete an element, you must ensure that the iterator is not an iterator that points to end.

C.erase (p)           removes the element that the iterator p points to. Returns an iterator that points to the element following the deleted element. If P points to the last element within the container, the returned iterator points to the next position beyond the end of the container.
If P itself is the next position beyond the end of the container, the function does not define C.ERASE (b,e) to Remove all elements in the range marked by Iterators B and E. Returns an iterator that points to the element following the deleted element segment. If e itself is pointing to the next position beyond the end of the container,
The returned iterator also points to the next position at the end of the container
C.clear ()            deletes all the elements in container c. Returns Voidc.pop_back ()         removes the last element of container C. returns void. If the container is empty, the function does not define C.POP_FRONT () to        remove the first element of container C. Women would void. If the container is empty, the function is undefined. applies To list and deque containers only

  

6. Assignment of containers and swap operations

The following table is an assignment operation for a sequential container. The left and right containers are equal after the assignment, although the size of the two containers is different before the assignment, but the length of the two containers after the assignment is the length of the starboard operand.

c1=C2                deletes all elements of the container c1, and then copies the C2 elements to C1. The types of C1 and C2 must be the same c1.swap (C2) to          Exchange the contents of C1 and c2, and the types of C1 and C2 must be the same. It is more efficient than copying the C2 element into the C1 to be High c.assign (b,e) to        Reset the elements of C: Copy the elements in the iterator B and E tags to c. B and E must not be an iterator pointing to elements in C c.assign (n,t) to        set container C back to store n elements with a value of T

C + + sequential container Knowledge Summary

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.