C + + Primer sequential container

Source: Internet
Author: User

Sequential containers: vector deque List

Adapters: According to the operations provided by the original container type, adapt the base container type by defining a new operating interface.

Container element initialization:

C<t> C; Default constructor

C<t>c (n); N-value initialization, only applies to sequential containers, must give default constructor

C<t>c (n,t); N-T is only applicable to sequential containers, can not be given, but to give the T (T) constructor.

C<t>c (Begin,end); Begin,end Copy to C

C<t> C1 (C2);//c1 and C2 are the same type of container, holding the same type of element, C2 copy to C1

If you want to assign the elements of different containers to each other, you can use the 4th constructor or assign.

The iterator behaves like a pointer.

Use as a container type to follow 2 conditions

1. Can assign value operation.

2. Can copy.

Note: Reference types cannot copy and assign values. The IO stream also does not support replication.

Vector deque Random Storage

List Chain List

Note the iterator failure problem.

Difference_type a signed integer that stores two iterator difference values that can be negative

Value_type element type

Reference element left value type,value_type&

Const_reference element constant left value type, const value_type&

Size_type unsigned integral type

Const_iterator

Reverse_iterator

Const_reverse_iterator

Always update iterators, or there will be an iterator failure problem.

Inserts are inserted in front of ITER.

Iterator insert (iter,n); N Insert ITER front, return iterator for new element

void Insert (iter,begin,end); The begin end is inserted in front of ITER.

void Insert (ITER,N,T); N T inserts ITER before.

If the container element supports a comparison character, you can make the container overall comparison.

such as Vector<int> c1,c2; C1==C2;

C.size ()

C.max_size ();

C.empty ();

C.resize (n); Change the length of C

C.resize (n,t);

Reference C.back (); Get a reference to the last element

Reference C.front (); Get a reference to the front element

c.at (n) ==c[n];

C.clear ()/Empty

C.erase (ITER); Return the elements behind ITER.

C.erase (begin,end)//return elements after Begin,end

Pop_front () and front () are often used together.

Iterator find (begin,end,value);//Locate the first place where value appears.

String-added constructor: An array with the end of a null character CP

1.string s (cp,n); The first n elements of the array that the CP points to.

2.string s (s2,pos2), copying from Pos2 to s in//s2

3.string s (s2,pos2,len), copying Len characters from Pos2 to S in//s2

string-specific functions:

1.s.insert (pos,s2); Insert string object before//pos,,, Pos int

2.s.insert (Pos,s2,pos,len); Len length inserted s2 from POS before//pos

3.s.insert (Pos,cp,len); before inserting the first Len character of the CP before//pos

4.s.insert (POS,CP); Insert CP before//pos

5.s.assign (S2); S2 assignment to S

6.s.assign (Cp,len); Len character of//CP to S

7.s.assign (CP);//CP assigned to S

8.s.erase (Pos,len)//delete len length from Pos

9.s.substr (pos,n);//Returns a string string containing the n characters starting at the Pos.

10.S.SUBSTR (POS);//Returns a string string containing the starting from POS

11.S.SUBSTR ()//return copy of S

Append and replace functions in string, returns the reference to S

1.s.append (ARG);//inserts ARG at the back of S.

2.s.replace (Pos,len,args)//replaces the args element with the character that the Pos begins with Len.

3.s.replace (B,e,args);//args elements Replace all b,e characters

The args can be:

1.s2

2.s2,pos,len

3.cp

4.cp,len

5.n,c

6.b2,e2; Iterators

Lookup function provided by string

1.s.find (args);

2.s.rfind (args);

3.s.find_first_of (args); the position of the first occurrence of any character in the//args

4.s.find_last_of (args);

5.s.find_first_not_of (args)//Find the first character that does not belong to args

6.s.find_last_not_of (args);/last character not belonging to Args

Args is:

1.c,pos//start from POS, find character C, default pos=0

2.s2,pos//pos start looking for string s2 pos defaults to 0

3.cp,pos//pos start looking for CP (CString) POS defaults to 0

4.cp,pos,n//pos starts looking for the first n characters of the array that the CP points to

String's compare function

1.s.compare (S2);

2.s.compare (POS1,N1,S2); N1 characters starting from pos1 compared to S2

3.s.compare (POS1,N1,S2,POS2,N2); N1 characters starting with pos1 compared to N2 characters starting with S2 from Pos2

4.s.compare (CP);//compared with char*

5.s.compare (POS1,N1,CP)//N1 characters from POS1 to CP comparison

6.s.compare (POS1,N1,CP,N2); The N1 characters starting with//POS1 are compared to the pre-N2 characters of the CP.

Adapters: Container adapters, iterator adapters, function adapters

Default

Deque Stack and queue

Vector priority_queue

Of course you can change the associated container.

such as Stack<string,vector<string>>

The associated container for the stack is a sequential container

The associated container for the queue must support Push_front ();

Priority_queue associated containers must provide random access.

Priority_queue sets Precedence inserts.

Related Article

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.