Vector sequences container ----

table of Contents

Introduction

1 Create

2 capacity and size

  size()

  capacity()

3 resize()

4 reverse()

Gets the element 5

  front()

  back()

6 iterator (to be supplemented)

7 push_back()

8 emplace_back()

9 emplace () (to be supplemented)

10 insert()

11 clear()

12 remove()

13 pop_back()

14 shrink_to_fit()

15 erase()

 

 

 

Introduction

And array <T, N> similar containers, except that the size of the vector <T> of the container may automatically increase. Most of the time, you can use vector <T> instead of an array of storage elements. As long as can be appreciated, vector <T> extensions capacity, and it will produce remove or add elements within the sequence number overhead; but in most cases, the code will not be significantly slower.

To use vector <T> container template files need to include in the code vector.

 

1 Create

Vector < int > A;                                 // declare a type int vector A 
Vector < int > A ( 10 );                             // declare an initial vector of size 10 
Vector < int > A ( 10 , . 1 );                          // declare an initial size 10 and the initial values of vector 1 
vector < int > B (a);                              // declaration and initialization vector with a vector B 
vector < int > B (a.begin (), a.begin () + . 3 );         / / to a vector from 0 to 2 (of 3) as an initial value of the vector b

Array initialization:

int n [] = { . 1 , 2 , . 3 , . 4 , . 5 }; 
Vector < int > a (n, n + . 5 );               // The first five elements of the array as a vector a n Initial 
Vector < int > a (n-& [ . 1 ], n-& [ 4 ]);         // the n [1] - n elements in the range [4] as the initial value of a vector

 

2 capacity and size

capacity of the vector magnitude, the most is the number of elements without allocate more memory can be saved, and the vector size is the actual number of elements it contains, i.e. the number of elements have a value.

 

 

 vector of size can not exceed its capacity directors. When the size equal to the size, will lead to an additional element to allocate more memory.

(1)size()

Returns the size, unsigned integer value

(2)capacity()

Return capacity, unsigned integer value

 

3 resize()

Change the size of the container.

Vector :: STD < int > values { . 1 , 2 , . 3 }; 
values.resize ( . 5 );
 // the number of elements becomes a specified value of the parameter, the two elements will increase initialized with default values.
// If you add an element, resulting in more than the current capacity of the vessel King, capacity will automatically increase. 

values.resize ( 7 , 99 );
 // container size to 7 and 99 to initialize with new elements
 // If 7 <original size of the container, then remove the extra element

 

4 reverse()

Changing the capacity of the container (number of elements, integers).

 

Gets the element 5

The subscript Index []

(1)front

Returns the first element of reference

(2)back

It returns a reference to the last element

 

Because the member function front () and back () returns a reference, so they can appear on the left side of the assignment operator.

values.front() = 2.71828;

 

6 iterator

1  // all outputs 
2 Vector < int > :: Iterator T;
 . 3  for (T = a.begin ();! A.end = T (); T ++) // T iterators, represents the position of elements may also be moving back and forth 
. 4      COUT t * << << "  " ; // * t indirect access in the form of a pointer is directed to access element value t

 

7 push_back()

 

8 emplace_back()

emplace_back () parameter of the parameter is added to the container object constructor needs.

emplace_back () with the parameter as its argument to the constructor, generating object in the container. If you do not want to use mobile computing, using push_back ().

() Function used in as many parameters as emplace_back, as long as they meet the requirements object constructor. Here is an example of using multi-parameter emplace_back () is:

:: STD string STR { " Alleged " }; 
words.emplace_back (STR, 2 , . 3 );
 // generates a string object (2 Alleged index composed of three elements start): leg, leg and then add words to the characters string back

 

9 emplace()

By using the member functions emplace (), you can insert a new element in the vector sequence. Object generated directly in the container, instead of generating the first object individual, and then it is passed as a parameter.

 

10 insert()

 

11 clear()

std::vector<int> data(100, 99);// Contains 100 elements initialized to 99
data.clear(); // Remove all elements

Removal of all the elements, the size becomes 0, because this operation does not change the capacity of the container, the capacity is 100.

 

12 remove()

remove () algorithm in the algorithm defined by the template generated header file, it can remove a particular value matches the period of the element.

 

13 pop_back()

 

14 shrink_to_fit()

Remove the excess capacity of the container, for example not add a new element to the vessel.

data.shrink_to_fit();

 

15 erase()

Delete the container one or more elements. If you delete only a single element, you only need to provide a parameter

auto iter = data.erase(std::begin(data)+1); //Delete the second element

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11441882.html