Directory [-]
Description
Structure
Method
Example
Four constructor types are defined in the vector class.:
·Default constructorTo construct an empty vector with an initial length of 0, for example, vector <int> V1; · constructor with a single integer parameter. This parameter describes the initial size of the vector.
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: int init_size; its member values are initialized to 0;
·Copy constructorTo construct a new vector, which is used as the complete copy of an existing vector;
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] (the display of MS word may be problematic, first is a left square bracket, 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) assigns data in the (beg; End) interval 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>
Using namespace STD;
Typedef vector <int> intvector; // custom type intvector
// Test the function of the vector container.
Void main (void)
{
// The vec1 object is initially empty.
Intvector vec1;
// The vec2 object initially has 10 elements with a value of 6.
Intvector vec2 (10, 6 );
// The vec3 object initially has 3 elements with a value of 6, copying the structure
Intvector vec3 (vec2.begin (), vec2.begin () + 3 );
// Declare a bidirectional iterator named I
Intvector: iterator I;
// Display data in vec1 from front to back
Cout <"vec1.begin () -- vec1.end ():" <Endl;
For (I = vec1.begin (); I! = Vec1.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Display data in vec2 from the beginning to the back
Cout <"vec2.begin () -- vec2.end ():" <Endl;
For (I = vec2.begin (); I! = Vec2.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Display data in vec3 from front to back
Cout <"vec3.begin () -- vec3.end ():" <Endl;
For (I = vec3.begin (); I! = Vec3.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Test the function for adding and inserting members. The vector cannot be inserted before.
Vec1.push _ back (2); // Add a member from the end
Vec1.push _ back (4 );
Vec1.insert (vec1.begin () +); // insert member 5 at the first position of vec1
// Insert all members of vec3 from the first position of vec1
Vec1.insert (vec1.begin () + 1, vec3.begin (), vec3.end ());
Cout <"after push () 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; // The 0th member of vec1
Cout <"vec1.back () =" <vec1.back () <Endl; // The last member of vec1
Cout <"vec1. at (4) =" <vec1. at (4) <Endl; // the fifth member of vec1
Cout <"vec1 [4] =" <vec1 [4] <Endl;
// Test removal and deletion. // at this time, vec1 is: 2 6 6 6 5 4.
Vec1.pop _ back (); // remove the last Member from vec1 // at this time, vec1: 2 6 6 5
Vec1.erase (vec1.begin () + 1, vec1.end ()-2 );// Delete a member --// Begin () + 1 indicates the first 6 from left to right, and end ()-2 indicates the 6 s from right to left, after the result of erase, vec1 is set to 2, 6, and 5.
Cout <"vec1.pop _ back () and vec1.erase ():" <Endl;
For (I = vec1.begin (); I! = Vec1.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Display 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.