C + + sequential containers (2)

Source: Internet
Author: User

1. Adding elements to a sequential container

C.push_back (t); C.emplace_back (args);//Creates a value of t or an element created by args at the tail of C, returning void. If on the head, back to front

C.insert (p,t); C,emplace (P,args); C.insert (p,n,t);(insert N) c.insert (p,b,e);(iterator range) C.insert (P,I1); (list)

Note: A. In addition to array and forward_list, each sequential container supports push_back.

B. Only List,forward_list,deque support Push_front

C.vector,list,deque,string supports INSERT, but other insert operations are time-consuming, except lists

D.string is a character container

E. If you pass a pair of iterators to insert, you cannot point to the target container where the element is added

F.while (Cin>>word) Iter=lst.insert (Iter,word);//loop inserts a new element, and ITER is updated every time to the previous position of the new element

G. When using Emplace, it is the constructor that passes parameters to the element type. When you use Insert and push, you copy an object of the element type to the container.

2. Accessing elements

C.back (); C.front (); References to first and trailing elements, C not null, back not for Forward_list

C[n]; c.at (n); N cannot be crossed , applicable to String,vector,deque,array

Note:

A. The access member function returns a reference (such as Front,back, subscript, and at)

3. Deleting an element

C.pop_back (); C.pop_front (); C.erase (P); C.erase (b,e); C.clear ();

4. Special Forward_list operation

Because Forward_list is a one-way linked list, deleting an element changes the link of the previous element, so its action function is:

Lst.before_begin (); Lst.cbefore_begin (); Lst.insert_after (p,t); Lst.insert_after (p,n,t); Lst.insert_after (p,b,e); Lst.insert_after (P,I1);

Emplace_after (P,args); Lst.erase_after (P);//delete an element after P Lst.erase_after (b,e);

When you add or remove elements from the above function, you are only interested in two iterators. One is the element to be processed, and the other is the precursor.

5. Change the size of the container

C.resize (n);//Adjust the size of C to N, if N<c.size (), the extra element is discarded, otherwise add a new element class.

C.resize (n,t);//Any newly added element is initialized to T

6. Container operation invalidates the iterator

Add to:

If it is a vector or string, the pointer, iterator, and reference before the insertion position is still valid, after which the invalidation

Deque, inserted into the middle, will fail.

Both list and forward_list are valid

Delete is similar to add.

How the 7.vector object grows

Vectors store elements continuously, but move other elements when inserting elements. So when you create, you reserve some space to hold more new elements.

C.shrink_to_fit ();//reduce capacity () to size () for vector, string, deque

C.capacity ();//c the number of elements that can be saved C.reserve (n); Allocates space that can hold at least n elements

C + + sequential containers (2)

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.