"C++primer (fifth Edition)" Learning Road-Chapter Nineth: Sequential Container __c++

Source: Internet
Author: User

"Statement: All rights reserved, reproduced please indicate the source, do not use for commercial purposes." Contact Email: libin493073668@sina.com "


9.1 Sequence Container Overview


1. Sequential container type

Vector variable size array. Support for fast random access. Inserting or deleting elements in a position other than the tail may be slow.

Deque a two-terminal queue. Support for fast random access. Inserts/deletes at the beginning and ends are very fast.

List doubly linked list. Only bidirectional sequential access is supported. Insert/delete operations are fast at any point in the list.

Forward_list one-way linked list. Only one-way sequential access is supported. Insert/delete operations are fast at any point in the list.

Array fixed size arrays. Support for fast random access. Elements cannot be added or deleted.

String is a container similar to vector, but is designed to hold characters. Random access fast. Inserts/deletes quickly at the tail.


2. The following are some of the basic principles for selecting containers

⑴. Unless you have a good reason to choose another container, you should use the vector

⑵. If your program has many small elements, and the extra overhead is important, do not use list or forward_list

⑶. If the program requires random access to elements, use vectors or deque

⑷. If the program needs to insert or delete elements at the top and the center, use the list or forward_list

⑸. If the program needs to insert or delete elements at the top and the center, but does not insert or delete in the middle position, use the deque

⑹. If the program only needs to insert elements in the middle of the container before reading the input, then you need to randomly access the elements.

--First, determine if you really need to add elements to the intermediate location of the container. When processing input data, it is usually easy to append data to the vector, and then call the standard library's sort function to rearrange the elements in the container to avoid adding elements in the middle.

-If you have to insert an element in the middle, consider using the list in the input phase, and once the input is complete, copy the contents of the list into a vector


9.2 Container Library Overview


1. Container operation

Type Alias

Iterator type of iterator for this container type

Const_iterator can read elements, but cannot modify the iterator type of elements

Size_type unsigned integer type, sufficient to hold the maximum possible container size for this type of container

Different_type signed integer type enough to hold the distance between two iterators

Value_type element type

The left value type of the reference element; same as the value_type meaning

Const_reference element's const left value type

Constructors

c C; Default constructors, constructing empty containers

C C1 (C2); Construct a copy of C2 C1

C c (b,e); Construct c, copy elements from the specified range of iterators B and E to C (array not supported)

C C (A,b,c ...); List Initialization C

Assignment and Swap

C1 = C2 replaces elements in C1 with elements in C2

C1 = {A,b,c.} To replace an element in a c1 with an element in a list

A.swap (b) exchanging elements of A and B

Swap (A,B) is equivalent to A.swap (b)

Size

Number of elements in C.size () c

Maximum number of elements that can be saved by c.max_size () c

C.empty () returns False if the element is stored in C, or returns True

Add/Remove elements (not applicable to array)

Note: In different containers, the interfaces of these operations are different

C.insert (args) copies the elements of args into C

C.emplace (inits) uses an element of the Inits construct C

C.erase (args) deletes the element specified by args

C.clear () deletes all elements in C, returning void

Relational operators

==,!= returns an iterator that points to the position of the first and end elements of C

<,<=,>,>= Relational operator (unordered association not supported for the same period)

Get iterators

C.begin (), C.end () returns an iterator that points to the position after the first and end elements of C

C.cbegin (), C.cend () return to Const_iterator

Additional members of the reverse container

Reverse_iterator an iterator that addresses the elements in reverse order

Const_reverse_iterator cannot modify the reverse iterator of an element

C.rbegin (), C.rend () returns an iterator that points to the end element of C and the position before the first element

C.crbegin (), C.crend () return to Const_reverse_iterator


2. Container definition and initialization

C C default constructor. If C is an array, the elements in C are initialized by default, or C is empty

C C1 (C2) C1 initialized to C2 copy. C1 and C2 must be of the same type.

C C1 = C2

C c{a,b,c ...} C is initialized to initialize the copy of the elements in the list. The type of the element in the list must be compatible with the element type of C. For array

c C = {A,b,c.} Type, the number of elements in the Leibiao must be equal to or less than the size of the array, and any missing elements are initialized with values.

C C (b,e) c is initialized to a copy of the elements in the specified range for iterators B and E. In scope

C seq (n) SEQ contains n elements that are initialized with values; This constructor is explicit

C seq (n,t) SEQ contains n elements that are initialized to the value T


3. Container Assignment operation

C1 = C2 replaces elements in C1 with copies of elements in C2. C1 and C2 must have the same type

c = {A,b,c.} Replace elements in C1 with copies of elements in the initialization list

Swap (C1,C2) to exchange elements in C1 and C2. C1 and C2 must have the same type. Swap is usually much faster than copying elements from C2 to C1.

C1.swap (C2)

Assign operation does not apply to associative containers and array

Seq.assign (B,E) replaces elements in SEQ with elements in the range represented by Iterators B and E. Iterators B and E cannot point to elements in SEQ

Seq.assign (IL) replaces elements in SEQ with elements in the initialization list IL

Seq.assign (N,T) replaces elements in SEQ with n-valued elements of T


9.3 Order Container


1. Actions to add elements to a sequential container

These operations change the size of the container, and the array does not support these operations.

Forward_list has its own wandering version of insert and emplace;

Forward_list does not support Push_back and emplace_back.

Vectors and strings do not support Push_front and Emplace_front.

C.push_back (t) creates an element with a value of t or created by args on the tail of C, returning void

C.emplace_back (args)

C.push_front (t) creates an element with a value of t or created by args in the head of C. returns void

C.emplace_front (args)

C.insert (p,t) creates an element that has a value of t or created by args before the element that the iterator p points to. Returns the iteration that points to the newly added element

C.emplace (P,args) generation device.

C.insert (p,n,t) inserts an element of n that has a value of t before the element that the iterator p points to. Returns the iterator that points to the newly added first element, or returns p if n is 0

C.insert (p,b,e) Inserts elements in the range specified by Iterators B and E before the element that the iterator p points to. B and E cannot point to elements in C. Returns an iterator that points to the newly added first element, or returns a p if the range is empty

C.insert (p,il) Il is a list of element values surrounded by curly braces. Inserts these given values before the element that the iterator p points to. Returns an iterator that points to the newly added first element, or returns a p if the list is empty


2. Accessing an element's operations in a sequential container

The at and subscript operations apply only to String,vector,deque and array

Back does not apply to Forward_list

C.back () returns a reference to the tail element in C. If C is empty, the function behavior is undefined

C.front () returns a reference to the first element in C. If C is empty, the function behavior is undefined

C[n] Returns a reference to the element labeled N in C and n is an unsigned integer. If N>=c.size (), the function behavior is undefined.

c.at (n) returns a reference to the element with subscript N. If the subscript is out of bounds, a Out_of_range exception is thrown.


3. Delete operation of sequential container

These operations change the container size, so they do not apply to array

Forward_list has a special version of erase.

Forward_list does not support Pop_back

Vector and string do not support Pop_front

C.pop_back () deletes the C-middle-end element. If C is empty, the function behavior is undefined. function returns void

C.pop_front () deletes the first element in C. If C is empty, the function behavior is undefined. function returns void

C.erase (p) deletes the element specified by the iterator p, returns an iterator that points to the element after the deleted element, and returns the trailing (off_the_end) iterator if p points to the tail element. If P is a trailing iterator, the function behavior is undefined.

C.erase (b,e) deletes elements in the range specified by Iterators B and E. Returns an iterator that points to the element after the last deleted element, and if E itself is a trailing iterator, then the function returns the rear iterator

C.clear () deletes all elements in C. returns void


4. Insert or delete an element in Forward_list

Lst.before_begin () returns an iterator that points to elements that do not exist before the first element of the list. This iterator cannot dereference. Cbefore_begin ()

Lst.cbefore_begin () returns a const_iterator

Lst.insert_after (p,t) inserts an element at the position after the iterator P, T is an object, n is the quantity, B and E is the range

Lst.insert_after (p,n,t) a pair of iterators (b and E cannot point to LST), IL is a list of curly braces. Returns a pointer to the last

Lst.insert_after (P,b,e) an iterator for inserting elements. Returns p if the range is empty. If P is a trailing-back iterator, then the function behavior

Lst.insert_after (p,il) not defined

Emplace_after (P,args) uses args to create an element after the location specified by P. Returns an iterator that points to this new element. If P is a trailing iterator, the function behavior is undefined.

Lst.earse_after (p) Deletes the element after the position that P points to, or deletes the element from B to (but not including) E. Returns a

Lst.earse_after (B,e) points to the iterator of the element after the deleted element, and if no such element exists, returns the trailing iterator. If p points to the tail element of LST or a trailing iterator, the function behavior is undefined.


5. Sequential Container size operation

Resize does not apply to array

C.resize (n) adjusts the size of C to n elements. If N<c.size (), the extra elements are discarded. Initializes a value for the new element if a new element must be added.

C.resize (n,t) Adjusts the size of C to n elements. Any newly added elements are initialized to the value T.


6.

After adding elements to the container:

⑴. If the container is a vector or string and the storage space is reassigned, the iterator, pointer, and reference to the container will fail. If the storage space is not reassigned, the iterator, pointer, and reference to the element before the insertion position are still valid, but the iterator, pointer, and reference to the element after the insertion point will be invalidated.

⑵. For deque, inserting to any location other than the beginning and end causes the iterator, the pointer, and the reference to be invalidated. If you add an element at the beginning and end, the iterator fails, but the reference and pointer to the existing element does not expire.

⑶. For the list and forward_list, the iterator that points to the container (including the rear iterator and the first-forward iterator), pointers, and references is still valid.


When we delete an element:

⑴. For lists and forward_list, iterators that point to other locations in the container, including end-post iterators and first-forward iterators, pointers, and references, are still valid.

⑵. For deque, if you delete an element anywhere other than the end, the iterator, reference, or pointer to any element other than the deleted element is invalidated. If you delete the tail element of the deque, the trailing iterator will also fail, but other iterators, references, and pointers are unaffected, and if you delete the first element, these will not be affected.

⑶. For vectors and strings, the iterators, references, and pointers to elements that precede the deleted element are still valid.

Note: When we delete an element, the rear iterator always fails.


how the 9.4 vector object grew


1. Container Size Management operations

Shrink_to_fit is used only for vectors, strings, and deque

Capacity and reserve apply only to vector and string

C.shrink_to_fit () Please reduce the capacity () to the same size ()

C.capacity () How many elements can c be saved without reallocating the memory space

C.reserve (n) allocates memory space that can hold at least n elements


2. When the added number exceeds the maximum capacity that the vector originally allocated, the strategy for the vector implementation is to double the current capacity each time a new memory space needs to be allocated.


9.5 Extra String Operations


1. Other methods of constructing string

string S (cp,n) s is a copy of the first n characters in the array to which the CP points. This array should contain at least n characters.

string S (s2,pos2) s is a copy of the character string S2 from the subscript pos2. If Pos2>s2.size (), the behavior of the constructor is undefined

The string s (s2,pos2,len2) s is a copy of the string S2 starting len2 characters from the subscript pos2. If Pos2>s2.size (), the behavior of the constructor is undefined. Regardless of the value of the Len2, the constructor copies S2.size ()-pos2 characters at most.


2. Sub-string operation

S.SUBSTR (Pos,n) returns a string containing a copy of n characters from the Pos beginning in S. The default value for POS is 0. The default value for n is s.size ()-pos, which copies all characters starting from the Pos.


3. Modify the operation of string

S.insert (Pos,args) inserts the args specified character before the Pos. A POS can be a subscript or an iterator. Accept the subscript version returns a reference to S; The version that accepts the iterator returns the iterator that points to the first caret character.

S.earse (Pos,len) deletes the Len character starting at the location POS. If Len is omitted, all characters from the beginning of the POS until the end of S are deleted. Returns a reference that points to S.

S.assign (args) replaces the characters in s with the characters specified by args. Returns a reference that points to s

S.append (args) appends the args to S. Returns a reference that points to s

S.replace (Range,args) deletes the characters in the range range in S and replaces the characters specified by args. Range is either a subscript and a length, or a pair of iterators pointing to S. Returns a reference that points to s


4.

The string class provides 6 different search functions, each of which has 4 overloaded versions.

Each search operation returns a String::size_type value that represents the subscript where the match occurred.

If the search fails, a static member named String::npos is returned.

The standard library defines NPOs as a const string::size_type type and is initialized to-1.

The string search function returns the String::size_type value, which is a unsigned type, so we should try not to use a signed type to hold these return values


5.

String search operation

S.find (args) finds the first occurrence of args in S

S.rfind (args) finds the last occurrence of args in S

S.find_first_of (args) finds the first occurrence of any character in args in S

S.find_last_of (args) finds the last occurrence of any character in args in S

S.find_first_not_of (args) finds the first character in s that is not in args

S.find_last_not_of (args) finds the last character in S that is not in args


6.

string comparison functions

S.compare (args)

Args form

S2                                 &NBS P  

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.