Tips for C ++ Primer Chapter 9 ordered container, primerchapter

Source: Internet
Author: User

Tips for C ++ Primer Chapter 9 ordered container, primerchapter
Chapter 2 Overview of ordered containers

Variable-size vector Array

Deque dual-end queue

List two-way linked list

Forward_list unidirectional linked list (C ++ 11)

Array fixed size array (C ++ 11)

A string is similar to a vector container, but is used to save characters.

The PS: C ++ 11 container is much faster than the old version, because the new standard supports "object movement" (Chapter 1 ).

 

Container repository Overview

Operations provided by all container types:

Type alias

Iterator the container type iterator type,

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

Size_type unsigned integer type; enough to save the maximum possible container size of this container type

Difference_type is a signed integer type that stores the distance between two iterators.

Value_type element type

The left Value Type of the reference element; equivalent to value_type &

The left Value Type of const_reference; that is, const value_type &

Constructor

C c default constructor; construct an empty container (for array)

C c1 (c2) to construct a copy of c2 c1

C c (B, e) constructs c; copies the elements in the range specified by iterator B and e to c (array is not supported)

C c {a, B, c...} List initialization c

Assignment and swap

C1 = c2 Replace the element in c1 with the element in c2

C1 = {a, B, c...} Replace the element in c1 with the element in the list (array is not supported)

A. swap (B) swap elements of a and B

Swap (a, B) is equivalent to a. swap (B ).

Size

C. size () number of elements in c (forward_list not supported)

C. maxsize () c. Maximum number of elements that can be saved

C. empty () c. returns true if no elements are stored.

Add/delete elements (not supported by array)

Note: In different containers, these operations have different interfaces.

C. insert (Args) SetArgsTo c.

C. emplace (Inits) UseInitsConstruct an element in c

C. erase (Args) DeleteArgsSpecified Element

C. clear () delete all elements in c; return void

Relational operators

=! = All containers support

<<=>> = Unordered associated containers are not supported.

Get iterator

C. begin () c. end () returns the iterator pointing to the position following the first and last elements of c.

C. cbegin () c. cend () returns const_iterator

Extra members of the reverse container (forward_list not supported)

Reverse_iterator iterator for backward addressing elements

Const_reverse_iterator cannot modify the reverse iterator of an element.

C. rbegin () c. rend () returns the iterator pointing to the end element of c and the position before the first element.

C. crbegin () c. crend () returns const_reverse_iterator

PS: The ++ operation of the reverse iterator gets the previous position.

 

Iterator

-- The operator is not applicable to forward_list;

The arithmetic operation of the iterator (Chapter 1) can only be applied to the iterator of string, vector, deque, and array;

If begin and end are equal, the range is null; otherwise, the range contains at least one element;

 

Among the functions that obtain the iterator, functions that do not start with c are overloaded; (begin, end, rbegin, rend)

When we call these functions for a very large object, we get the version of iterator. Only when we call these functions for a const object will we get a version of const;

You can convert a common iterator to a corresponding const_iterator.

 

When auto is used in combination with begin or end, the obtained iterator type depends on the container type. Versions starting with c must obtain const_iterator, regardless of the container type.

Auto it1 = c. begin (); // only when c is const, it1 is const_iterator

Auto it2 = c. cbegin (); // it2 is const_iterator

 

Container definition and initialization

C c default constructor; if C is an array, the elements in c are initialized by default; otherwise, c is empty.

C c1 (c2) C c1 = c2 c1 a copy initialized to c2. c1 and c2 must be of the same container type and element type. For array type, the two must have the same size.

C c {a, B, c ...} C c = {a, B, c ...} c is initialized to copy the elements in the initialization list. The element type in the list must be compatible with the element type in C. For the array type, the number of elements in the list must be equal to or less than the array size, the missing elements will be initialized (Chapter 1)

C c (B, e) c is initialized as a copy of the elements in the specified range of iterator B and e. The element types in the range must be compatible with the element types of C. array does not support

Below, only the constructors of the ordered container (excluding array) can accept the size parameter:

C seq (n) seq contains n elements whose values are initialized; this constructor is explicit; string is not supported, array is not supported

C seq (n, t) seq contains n t-valued elements; supported by string and not supported by array

PS: If the element type is a built-in type or a class type with a default constructor, you can use the first method. If the element type does not have a default constructor, you can only use the second method.

 

Standard Library array has a fixed size

Like the built-in array, the size of the standard library array is also part of the type.

When defining or using an array, you must specify the container size in addition to the element type.

Array <string, 10> // type: the array that saves 10 strings

Array <string, 10 >:: size_type I; // OK

Array <string >:: size_type j; // error

 

Unlike other containers, an array constructed by default is non-empty: it contains the same elements as its size. These elements are initialized by default. (Like a built-in array)

If you initialize the list of arrays, the initial value must be equal to or smaller than the array size. If it is smaller than the array size, the back element will initialize the value.

In the preceding two cases, if the element type is a class type, the class must have a default constructor.

 

Different from the built-in array type, array supports copying or assigning values to objects. When copying or assigning values to an array, ensure that the element types and number of the two objects are the same.

Array <int, 3> a {0, 1, 2 };

Array <int, 3> B = a; // OK

Array <double, 3> c = a; // error; although int conversion to double is acceptable

 

Assignment and swap

C1 = c2; // the content and size of c1 change to the same as that of c2.

C1 = {a, B, c} // the size of c1 is 3

 

Array does not support assign or assign values to the list of values enclosed by curly brackets. (In fact, Some compilers support assigning values to Arrays Using the initialization List)

 

Container assignment

In c1 = c2 c1, replace the element with the copy of the element in c2. c1 and c2 must be of the same type. After the value is assigned, the size of c1 is the same as that of c2.

C1 = {a, B, c ...} replace the element in c1 with the copy of the element in the initialization list. For array, Some compilers support this operation (for example, gcc 4.9.2-std = c ++ 11), and some do not support this operation.

Swap (c1, c2) c1.swap (c2) Exchanges elements in c1 and c2. swap is usually much faster than copying elements from c2 to c1.

(PS: swap only exchanges the internal data structure of the two containers, and the elements themselves are not exchanged. Except for the array, swap does not copy, delete, or insert any elements, therefore, swap is a constant time .)

(PS: swap of the previous version is added in C ++ 11)

Following, the assign operation does not apply to associated containers and arrays:

Seq. assign (B, e) replaces the elements in seq with elements in the range of [B, e); iterator B and e cannot point to elements in seq

Seq. assign (li) replaces the element in seq with the element in the initialization list li

Seq. assign (n, t) replaces the elements in seq with n elements whose values are t.

PS: in the preceding example, seq indicates the object of an ordered container (excluding array ).

 

Assignment operations may cause the iterator, reference, and pointer pointing to the left container to fail;

The swap operation will not cause the iterator, reference, and pointer pointing to the container to become invalid. (Except when the container type is array or string)

Note: Because the swap operation does not actually move the elements in the container, except for the string, the iterator, reference, and pointer pointing to the container will not expire after the swap operation, they still point to those elements before the swap operation. However, after swap operations, these elements already belong to different containers.

Note: For arrays, swap operations will actually swap their elements, and the switching time is proportional to the number of elements. After swap operations, the pointer, reference, and iterator bound elements remain unchanged, however, the element value has been exchanged with the value of the corresponding element in another array.

 

Use assign (sequential container only, excluding array)

The value assignment operator requires that the objects on the left and right have the same type. An ordered container (except array) also defines a member named assign, which allows assignment from a different but compatible type and assignment from a sub-sequence of the container.

List <string> names;

Vector <const char *> oldstyle;

Names = oldstyle; // error; Container Type Mismatch

Names. assign (oldstyle. cbegin (), oldstyle. cend (); // OK; const char * can be converted to string

Names. assign (oldstyle. begin (), oldstyle. end (); // OK; char * can also be converted to string

PS: because the old elements are replaced, the iterator passed to the assign cannot point to the container that calls the assign.

 

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.