By default, all elements of each vector object are set to 0.
ex.
vector< int > integer1( 7 );
vector Member Function size; Function outputVector
every container except forward_List
ex.
cout << "Size of vector integers1 is " << integer1.size()
<< "\nvector after initialization:" << endl;
outputVector( integers1 );
Function outputVector
ex.
inputVector( integers1 );
Comparing vector Objects for Inequality
ex.
if ( integers1 != integers2 )
cout << "integers1 and integers2 are not equal " << endl;
Initializing One vector with Contents of Another
ex.
vector< int > integers3( integers1 );
Assigning vectors and comparing vectors for Equality
ex.
integers1 = integers2
integers1 == integers2
Using the [] Operator to Access and Modify vector Elements
Standard class template vector provides bounds checking in its member function at()
ex.
cout << "\nintegers1[5] is " << integers1[ 5 ] = 1000;
cout << integers1.at( 15 ) << endl;
Changing the Size of a vector
other than array and forward_list
integers3.push_back( 1000 );
vector Member Function capacity
specific to vector and deque
ex.
cout << "\nThe initial capacity of integers is: " << integers.capacity();
Outputting vector Contents with Iterators
ex.
1. C++11
for ( auto constIterator = integers2.cbegin(); constIterator != integers2.cend(); ++constIterator )
cout << *constIterator << ' ';
could have been replace with the following range-based for statement:
for ( auto const &item : integers2 )
cout << item << '';
2. prior to C+11
vector<int>::const_iterator
Displaying the vector's Content in Reverse with const_reverse_iterators
for ( auto reverseIterator = integers2.crbegin(); reverseIterator != integers2.crend(); ++reverseIterator )
cout << *reverseIterator << ' ';
C++11: shrink_to_fit
std::vector<int> myvector (100);
cout << "1. capacity of myvector: " << myvector.capacity() << '\n';
myvector.resize(10);
cout << "2. capacity of myvector: " << myvector.capacity() << '\n';
myvector.shrink_to_fit();
cout << "3. capacity of myvector: " << myvector.capacity() << '\n';
vector Element-Manipulation Functions
vector< int > integers{ 1, 2, 3, 4, 5, 6 };
or
vector< int > integers = {1, 2, 3, 4, 5, 6 };
using an overloaded vector constructor that takes two as arguments to initialize integers
array< int, SIZE > value = { 1, 2, 3, 4, 5, 6 }
vector< int > integers( value.cbegin(), values.cend() );
using an overloaded vector constructor that takes two as arguments to initialize integers
array< int, SIZE > value = { 1, 2, 3, 4, 5, 6 }
vector< int > integers( value.cbegin(), values.cend() );
沒有留言:
張貼留言