Reference http://dev.csdn.net/article/48/article/48/article/49/49091.shtm
Void cvectortest: showinfo (string name, vector <int> & vintvector)
{
// C. max_size () returns the maximum number of data in the container.
// C. At (idx) returns the data referred to by the index idx. If idx is out of bounds, out_of_range is thrown.
// C. Capacity () returns the number of data in the container.
// C. Size () returns the actual number of data in the container.
Cout <"**" <name <Endl;
Cout <"Size:" <vintvector. size () <"Capacity:" <vintvector. capacity () <"Max size:" <vintvector. max_size ()/(1024*1024) <"M" <Endl;
For (INT Index = 0; index <vintvector. Size (); index ++)
{
// Cout <"index:" <index <"value:" <vintvector [Index] <Endl;
}
// Iterator -- for any container except the vector, you can use this cursor to take a step forward from the container in one operation. This means that you can only use the "++" Operator for such cursors. You cannot use the "--" or "+ =" operators. For the vector container, you can use any operator in "+ =", "-", "+ +", "-=", and "<", "<=", "> ", "> =", "=", "! = "And other comparison operators.
// Reverse_iterator -- if you want to use a cursor in the backward direction instead of the forward direction to traverse elements in containers other than the vector, you can use reverse_iterator to reverse the traversal direction, you can also use rbegin () to replace begin () and rend () to replace end (). At this time, the "++" operator will traverse the back direction.
// Const_iterator -- a forward cursor that returns a constant value. You can use this type of cursor to point to a read-only value.
// Const_reverse_iterator -- a game traversing in the opposite direction
// C. Begin () returns the unique data of the iterator.
// C. End () points to the last data address in the iterator.
// C. rbegin () returns the first data of a reverse queue.
// C. rend () returns the next position of the last data in the reverse queue. Returns a constant value.
Vector <int>: iterator intiterator;
Cout <"iterator :";
For (intiterator = vintvector. Begin (); intiterator! = Vintvector. End (); intiterator ++)
{
Cout <* intiterator;
If (intiterator! = Vintvector. End ()-1)
Cout <",";
}
Cout <Endl;
Vector <int>: reverse_iterator intreiterator;
Cout <"reverse iterator :";
For (intreiterator = vintvector. rbegin (); intreiterator! = Vintvector. rend (); intreiterator ++)
{
Cout <* intreiterator;
If (intreiterator! = Vintvector. rend ())
Cout <",";
}
Cout <Endl;
// C. Front () returns a data record.
// C. Back () returns the last data and does not check whether the data exists.
// C. Empty () determines whether the container is empty.
If (vintvector. Empty ())
{
Cout <"empty vector." <Endl;
}
Else
{
Cout <"first element:" <vintvector. Front () <Endl;
Cout <"last element:" <vintvector. Back () <Endl;
}
}
Void cvectortest: Test ()
{
Vector <int> vector0;
For (INT I = 20; I <= 28; I ++)
{
Vector0.push _ back (I );
}
Showinfo ("vector0", vector0 );
//////////// Value of data in the vector
// Vector <ELEM> C creates an empty vector.
Vector <int> vector1;
// C. Assign (beg, end) assigns the data in the [beg; End) range to C.
Vector1.assign (vector0.begin (), vector0.end ());
Showinfo ("vector1", vector1 );
// C. Assign (n, ELEM) assigns the copy value of n elem to C.
Vector1.assign (3, 5 );
Showinfo ("vector1", vector1 );
/////////// Vector structure
// Vector <ELEM> C1 (C2) copies a vector.
Vector <int> vector2 (vector1 );
Showinfo ("vector2", vector2 );
// Vector <ELEM> C (n) creates a Vector Containing N pieces of data, which are generated by default.
Vector <int> vector3 (6 );
Showinfo ("vector3", vector3 );
// Vector <ELEM> C (n, ELEM) creates a Vector Containing n elem copies.
Vector <int> vector4 (4, 8 );
Showinfo ("vector4", vector4 );
// Vector <ELEM> C (beg, end) creates a vector in the [beg; End) interval.
Vector <int> vector5 (vector1.begin () + 1, vector1.end ()-1 );
Showinfo ("vector5", vector5 );
//////////// Exchange the elements in two vectors
// Swap (C1, C2) SWAps C1 and C2 elements.
// Swap (C1, C2) is the same as above.
Vector2.swap (vector3 );
Showinfo ("vector2", vector2 );
Showinfo ("vector3", vector3 );
//// // Insert data
// C. insert (Pos, ELEM) inserts an ELEM copy at the POs position and returns the new data location.
Vector2.insert (vector2.begin () + 1, 3 );
Showinfo ("vector2", vector2 );
// C. insert (Pos, N, ELEM) inserts n ELEM data at the POs position. No return value.
Vector2.insert (vector2.begin () + 1, 2, 4 );
Showinfo ("vector2", vector2 );
// C. insert (Pos, beg, end) data inserted in the [beg, end) range at the POs position. No return value.
Vector2.insert (vector2.begin () + 1, vector3.begin (), vector3.end ());
Showinfo ("vector2", vector2 );
//// // Add or delete data
// C. pop_back () Delete the last data.
// C. push_back (ELEM) Add a data at the end
For (I = 0; I <= vector0.size ()-1; I ++)
{
Vector0.pop _ back ();
}
Showinfo ("vector0", vector0 );
Showinfo ("vector1", vector1 );
//// // Delete data
// C. Erase (POS) deletes the data at the POs location and returns the next data location.
Vector1.erase (vector1.begin () + 2 );
Showinfo ("vector1", vector1 );
// C. Erase (beg, end) deletes the data in the [beg, end) interval and returns the next data location.
Vector1.erase (vector1.begin () + 1, vector1.end ()-1 );
Showinfo ("vector1", vector1 );
// C. Clear () removes all data from the container.
Vector1.clear ();
Showinfo ("vector1", vector1 );
////////// Compress the vector using the Switching Technique
Vector <int> (vector1). Swap (vector1 );
Showinfo ("vector1", vector1 );
////////// Change the volume of the Vector
// Reserve (container: size_type N) forces the container to change its capacity to at least N, and the provided n is not smaller than the current size. This forces a redistribution because the capacity needs to increase. (If n is smaller than the current capacity, vector ignores it. This call does nothing. String may reduce its capacity to size () and N in large numbers, however, the size of the string is not changed.
Vector0.reserve (50 );
Showinfo ("vector0", vector0 );
// Resize (container: size_type N) forces the container to accommodate n elements. After Resize is called, size returns n. If n is smaller than the current size, the elements at the end of the container are destroyed. If n is greater than the current size, the new default element is added to the tail of the container. If n is greater than the current capacity, it will be reassigned before the element is added.
Vector0.resize (2 );
Showinfo ("vector0", vector0 );
}