C ++ primer sequential container

Source: Internet
Author: User

Ordered container: vector deque list

Adapter: defines new operation Interfaces Based on the operations provided by the original container type to adapt to the basic container type.

 

Container element initialization:

C <t> C; // default constructor

C <t> C (n); // initialize N values,Only applicable to ordered containers. The default constructor must be provided.

C <t> C (n, T); // n tIt is only applicable to ordered containers. It can be left blank, but a T (t) constructor must be provided.

C <t> C (begin, end); // copy begin and end to C.

C <t> C1 (C2); // c1 and c2 are the same container type. They store elements of the same type, and C2 copies them to C1.

 

If you want to assign values to each other for different containers, you can use 4th constructors or use assign.

The behavior pointer of the iterator.

 

Two conditions must be observed when used as the container type.

1. assign values.

2. can be copied.

Note: The reference type cannot be copied or assigned values. Io streams do not support replication.

 

Vector deque Random storage

List linked list

 

Pay attention to the failure of the iterator.

 

Difference_type is a signed integer that stores the difference between the two iterators. It can be a negative number.

Value_type element type

Type of the Left value of the reference element, value_type &

Const_reference element constant left value type, const value_type &

Size_type unsigned integer

Const_iterator

Reverse_iterator

Const_reverse_iterator

 

Update the iterator at any time. Otherwise, the iterator becomes invalid.

Insert is inserted before ITER.

Iterator insert (ITER, n); // n Insert before ITER and return the iterator of the new element

Void insert (ITER, begin, end); // begin end is inserted before ITER.

Void insert (ITER, N, T); n t before ITER.

 

If the container element supports comparison characters, you can compare the entire container.

For example, 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 the reference of the last element

Reference C. Front (); // get the reference of the first element

C. At (n) = C [N];

 

C. Clear (); // clear

C. Erase (ITER); // return the elements following iter

C. Erase (begin, end); // returns the elements after begin and end.

 

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

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

 

Constructor added by string: An array whose CP ends with a NULL Character

1. String S (CP, n); // The first n elements of the array pointed to by CP.

2. String S (S2, pos2); // copy from pos2 to S

3. String S (S2, pos2, Len); // The LEN characters are copied from pos2 to S

 

String-specific functions:

1. S. insert (Pos, S2); // String object inserted before POs, where POS is int

2. S. insert (Pos, S2, POs, Len); // The LEN Length of S2 inserted before the POs

3. S. insert (Pos, CP, Len); // The first LEN character of CP inserted before POS

4. S. insert (Pos, CP); // insert CP before POS

5. S. Assign (S2); // S2 is assigned to S.

6. S. Assign (CP, Len); // The Len of CP is given to S

7. S. Assign (CP); // CP assigned to S

8. S. Erase (Pos, Len); // Delete the LEN Length starting from POS

 

 

9. S. substr (Pos, n); // returns a string containing n characters starting from POS.

10. S. substr (POS); // returns a string that contains

11. S. substr (); // returns a copy of S.

 

The append and replace functions in string, return s reference

1. S. append (ARG); // insert Arg behind S.

2. S. Replace (Pos, Len, argS); // Replace The args element with the characters whose POS start to be Len.

3. S. Replace (B, E, argS); // replace all characters of B and E with the element of argS.

 

ARGs can be:

1. S2

2. S2, POs, Len

3. CP

4. CP, Len

5. N, C

6. B2, E2; // iterator

 

Search functions provided by string

1. S. Find (ARGs );

2. S. rfind (ARGs );

3. S. find_first_of (ARGs); // location where any character in ARGs appears for the first time

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); // The last character that does not belong to args

 

ARGs:

1. c, POS // start from POs and search for character C. Default Pos = 0

2. S2, POS // POS start to search for string S2 pos. The default value is 0.

3. CP, POS // POS start to search for CP (cstring) pos. The default value is 0.

4. CP, POs, N // POS start to find the first n characters of the array pointed to by CP

 

Compare function of string

1. S. Compare (S2 );

2. S. Compare (pos1, N1, S2); // compare the N1 characters starting from pos1 WITH S2.

3. S. Compare (pos1, N1, S2, pos2, N2); // N1 characters starting from pos1 and N2 characters starting from pos2

4. S. Compare (CP); // compare with char *

5. S. Compare (pos1, N1, CP); // compare the N1 characters starting from pos1 with CP

6. S. Compare (pos1, N1, CP, N2); // compare the N1 characters starting with pos1 with the first N2 characters of CP.

 

 

Adapter: Container adapter, iterator adapter, function Adapter

Default

Deque stack and queue

Vector priority_queue

Of course, you can change the associated container.

For example, stack <string, vector <string>

The associated containers of stack are sequential containers.

The associated container of queue must support push_front ();

The container associated with priority_queue must provide random access.

Priority_queue sets priority for insert.

 

 

 

 

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.