This is available on the Internet at will, just Google and everything.
Description
Vector is a dynamic array and a class template for basic arrays. Many basic operations are defined internally.
# Include <vector> note: the header file does not contain ". H"
Structure:
This constructor also has an optional parameter, which is an instance of the T type and describes the initial values of each member of each vector;
For example, vector <int> V2 (init_size, 0); if it is pre-defined: intinit_size; its member values are initialized to 0;
· Copy constructor to construct a new vector, which serves as the full replication of existing vectors;
For example, vector <int> V3 (V2 );
· Constructor with two constant parameters to generate a vector with an initial value as an interval. The interval is specified by a semi-open interval [first, last] (MSWord may be displayed incorrectly, first is a left square bracket, and last is a right circle bracket.
For example, vector <int> V4 (first, last) vector <int>
V1;
Vector <int> V2 (init_size, 0 );
Vector <int> V3 (V2 );
Method:
C. Assign (beg, end) C. Assign (n, ELEM) Will (beg;
End) values in the range are assigned to C. Assign a copy of n elem values to C.
C. At (idx) returns the data referred to by the index idx. If idx is out of bounds, out_of_range is thrown.
C. Back () returns the last data and does not check whether the data exists.
C. Begin () returns the first data address in the iterator.
C. Capacity () returns the number of data in the container.
C. Clear () removes all data from the container.
C. Empty () determines whether the container is empty.
C. End () // points to the next of the End Element in the iterator and to a nonexistent element.
C. Erase (POS) // Delete the data at the POs location and return the next data location.
C. Erase (beg, end) deletes data in the [beg, end) interval and returns the location of the next data.
C. Front () returns the first data.
Get_allocator returns a copy using the constructor.
C. insert (Pos, ELEM) // insert an ELEM copy at the POs position to return the new data location
C. insert (Pos, N, ELEM) // insert n ELEM data at the POs position, no return value
C. insert (Pos, beg, end) // insert data in the [beg, end) interval at the POs position. No return value
C. max_size () returns the maximum number of data in the container.
C. pop_back () Delete the last data.
C. push_back (ELEM) adds a data at the end.
C. rbegin () returns the first data in a reverse queue.
C. rend () returns the next location of the last data in a reverse queue.
C. Resize (Num) re-specifies the queue length.
C. Reserve () retains the appropriate capacity.
C. Size () returns the actual number of data in the container.
C1.swap (C2) // swap C1 and C2 Elements
Example:
To help understand the concept of vectors, we have written a small example, where the member functions of vector are used: Begin (), end (), push_back (), assign (), front (), back (), erase (), empty (), at (), size ().
// Stl_cpp_8.cpp
# Include <iostream> # include <vector> usingnamespace STD; typedefvector <int> intvector; // test the voidmain (void) function of the vector container) {// The vec1 object is initially empty. intvectorvec1; // The vec2 object initially has 10 elements with 6 values: intvectorvec2 ); // The vec3 object initially has three elements with a value of 6. Copy and construct intvectorvec3 (vec2.begin (), vec2.begin () + 3 ); // declare a bidirectional iterator named I intvector: iteratori; // The data cout in vec1 is displayed from the front to the back <"vec1.begin () -- vec1.end ():" <Endl; for (I = vec1.begin (); I! = Vec1.end (); ++ I) cout <* I <"; cout <Endl; // The cout data in vec2 is displayed from the front to the back <" vec2.begin () -- vec2.end (): "<Endl; for (I = vec2.begin (); I! = Vec2.end (); ++ I) cout <* I <"; cout <Endl; // data cout in vec3 is displayed from the front to the back <" vec3.begin () -- vec3.end (): "<Endl; for (I = vec3.begin (); I! = Vec3.end (); ++ I) cout <* I <""; cout <Endl; // test adding and inserting member functions, vector cannot insert vec1.push _ back (2) from the beginning; // Add a member vec1.push _ back (4); vec1.insert (vec1.begin () + ); // insert member 5 at the first position of vec1 // insert all members of vec3, vec1.insert (vec1.begin () + 1, vec3.begin (), vec3.end (); cout <"afterpush () and insert () now the vec1 is:" <Endl; for (I = vec1.begin (); I! = Vec1.end (); ++ I) cout <* I <"; cout <Endl; // test the value assignment function vec2.assign (8, 1 ); // assign a value to vec2 again. The initial values of the eight members are 1 cout <"vec2.assign (8, 1):" <Endl; for (I = vec2.begin (); I! = Vec2.end (); ++ I) cout <* I <"; cout <Endl; // test the reference class function cout <" vec1.front () = "<vec1.front () <Endl; // cout <" vec1.back () = "<vec1.back () <vec1.back () <Endl; // cout <"vec1.at (4) =" <vec1. at (4) <Endl; // cout <"vec1 [4] =" <vec1 [4] = "<vec1 [4] <Endl; // test removal and deletion of vec1.pop _ back (); // remove the last Member from vec1 vec1.erase (vec1.begin () + 1, vec1.end ()-2); // Delete the member cout <"vec1.pop _ back () and vec1.erase (): "<Endl; for (I = vec1. Begin (); I! = Vec1.end (); ++ I) cout <* I <"; cout <Endl; // shows the sequence status information cout <" vec1.size (): "<vec1.size () <Endl; // print the number of Members. cout <" vec1.empty (): "<vec1.empty () <Endl; // determines whether vec1 is null. If it is null, 1 is returned. If it is not null, 0 is returned}
Push_back () is a standard function that puts data into a vector or deque (double-end Queue. Insert () is a similar function, but it can be used in all containers, but its usage is more complex. End () is actually adding one at the end to let the loop run correctly -- the pointer it returns points to the data closest to the array boundary.