vector usage finishing

vector often called a container, as a vector to accommodate other elements, all the elements of the same type. Each element has an associated index corresponding to the index used to access elements. Briefly, vector is a dynamic array can store many types of data, the position of the elements which are contiguous in memory.

Include the header file:

#include <vector>
using namespace std;

vector class is a template, it is necessary to provide the type of object stored within the vector instantiation:

vector<int> ivec;
vector<string> file;

 

 Definition and initialization vector objects

Vector <T> V1; // V1 is a null vector, the elements of its potential T-type 
Vector <T> V2 (V1); // equivalent to Vector <T> V1 = V2 
Vector <T> V3 (n-, Val); // V3 contains n elements, each of Val 
Vector <T> V4 (n); // V4 has n element values is performed initialization 
vector <T> v5 {a, b, c, ......} D; // V5 elements comprising the initial number of values, each element is assigned a corresponding initial value, equivalent to the vector <T> v5 = {a , b, c, d ......}

vector <T> v1, v1 does not contain any element, an empty container, it seems useless, but the program can be efficiently added to the vector elements at runtime, which is in fact a common method, defined as the ratio the size of the container is more efficient.

Initialization list or the number of elements?

Vector <T> V1 ( 10 ); // V1 has 10 elements, each values are 0 
Vector <T> V2 { 10 }; // V2 elements have a value of 10 

Vector <T> V3 ( 10 , 1 ); // V3 has 10 elements, each value of a 
Vector <T> V4 ( 10 , 1 ); // V4 has two elements, values of 10 and 1, respectively,

If you are using parentheses, it can be said value is used to construct vector objects; if it is braces, we can say we want the list to initialize the vector objects, that is, the initialization process will try to put a value in braces as handle element initial value, the initialization mode will be considered only when other types do not correspond. The vector <string> v8 {10, "hi"}; // v8 has the value 10 "hi" element.

Adding elements to a vector object

vector push_back member function responsible for as a value on a vector to the end of the last element vector pressed object.

vector < int > v2; // empty vector objects 
for ( int I = 0 ; I =! 100 ; I ++ ) 
    v2.push_back (I); // sequentially into the integer v2 tail

Other vector operations

v.empty (); // if v does not contain any element, returns true; false otherwise 
v.size (); // returns the number of elements in v 
v [n]; // Returns the n th position v the reference element 
<, <=,>,> =; // to compare lexicographically

Note : vector objects subscript operator can be used to access an existing element, the additional element can not be used.

vector < int > ivec; // Iveco empty vector objects 
for ( int I = 0; I =! 10 ; ++ I) 
    ivec [I] = I; // error, ivec contains no elements

 

Guess you like

Origin www.cnblogs.com/cs0915/p/12275997.html