Initialization and assignment
// Constructing Vectors
# Include <iostream>
# Include <vector>
Using Namespace STD;
Int Main ()
{
Unsigned Int I;
// Constructors used in the same order as described above:
Vector < Int > First; // Empty vector of ints
Vector < Int > Second ( 4 , 100 ); // Forur ints with value 100
Vector < Int > Third (second. Begin (), second. End ());// Iterating through second
Vector < Int > Fourth (third ); // A copy of third
// The iterator constructor can also be used to construct from Arrays:
Int Myints [] = { 16 , 2 , 77 ,29 };
Vector < Int > TH (myints, myints + Sizeof (Myints )/ Sizeof ( Int ));
Cout < " The contents of parameter th are: " ;
For (I = 0 ; I <th. Size (); I ++)
Cout < " " <Th [I];
Cout <Endl;
Return 0 ;
}
Traversal
// Vector: operator []
# Include <iostream>
# Include <vector>
Using Namespace STD;
Int Main ()
{
Vector < Int > Myvector ( 10 ); // 10 zero-initialized Elements
Unsigned Int I;
Vector < Int >:: Size_type SZ = myvector. Size ();
// Assign some values:
For (I = 0 ; I <SZ; I ++) myvector [I] = I;
// Reverse vector using operator []:
For (I = 0 ; I <sz/ 2 ; I ++)
{
Int Temp;
Temp = myvector [SZ- 1 -I];
Myvector [SZ- 1 -I] = myvector [I];
Myvector [I] = temp;
}
Cout < " Myvector contains: " ;
For (I = 0 ; I <SZ; I ++)
Cout < " " <Myvector [I];
Cout <Endl;
Return 0 ;
}
Append
// Vector: push_back
# Include <iostream>
# Include <vector>
Using Namespace STD;
Int Main ()
{
Vector < Int > Myvector;
Int Myint;
Cout <" Please enter some integers (enter 0 to end): \ n " ;
Do {
Cin> Myint;
Myvector. push_back (Myint );
} While (Myint );
Cout < " Myvector stores " <( Int ) Myvector. Size () < " Numbers. \ n " ;
Return 0 ;
}
Insert
// Inserting Into a vector
# Include <iostream>
# Include <vector>
Using Namespace STD;
Int Main ()
{
Vector < Int > Myvector ( 3 , 100 );
Vector < Int >:: Iterator it;
It = myvector. Begin ();
It = myvector. insert (it, 200 );
Myvector. insert (it, 2 , 300 );
// "It" no longer valid, get a new one:
It = myvector. Begin ();
Vector < Int > Anothervector ( 2 , 400 );
Myvector. insert (it + 2 , Anothervector. Begin (), anothervector. End ());
Int Myarray [] = { 501 , 502 , 503 };
Myvector. insert (myvector. Begin (), myarray, myarray + 3 );
Cout < " Myvector contains: " ;
For (It = myvector. Begin (); It <myvector. End (); It ++)
Cout < " " <* It;
Cout <Endl;
Return 0 ;
}
Delete
// Erasing from Vector
# Include <iostream>
# Include <vector>
Using Namespace STD;
Int Main ()
{
Unsigned Int I;
Vector <unsigned Int > Myvector;
// Set some values (from 1 to 10)
For (I = 1 ; I <= 10 ; I ++) myvector. push_back (I );
// Erase the 6th Element
Myvector. Erase (myvector. Begin () + 5 );
// Erase the first 3 elements:
Myvector. Erase (myvector. Begin (), myvector. Begin () + 3 );
Cout < " Myvector contains: " ;
For (I = 0 ; I <myvector. Size (); I ++)
Cout < " " <Myvector [I];
Cout <Endl;
Return 0 ;
}
Clear
// Clearing Vectors
# Include <iostream>
# Include <vector>
Using Namespace STD;
Int Main ()
{
Unsigned Int I;
Vector < Int > Myvector;
Myvector. push_back ( 100 );
Myvector. push_back ( 200 );
Myvector. push_back ( 300 );
Cout < " Myvector contains: " ;
For (I = 0 ; I <myvector. Size (); I ++) cout < " " <Myvector [I];
Myvector. Clear ();
Myvector. push_back ( 1101 );
Myvector. push_back ( 2202 );
Cout < " \ Nmyvector contains: " ;
For (I = 0 ; I <myvector. Size (); I ++) cout < " " <Myvector [I];
Cout <Endl;
Return 0 ;
}
More operations
Reference http://www.cplusplus.com/reference/stl/vector/