📄 fig20_14.cpp
字号:
// Fig. 20.14: fig20_14.cpp
// Testing Standard Library vector class template
#include <iostream>
#include <vector>
using namespace std;
template < class T >
void printVector( const vector< T > &vec );
int main()
{
const int SIZE = 6;
int a[ SIZE ] = { 1, 2, 3, 4, 5, 6 };
vector< int > v;
cout << "The initial size of v is: " << v.size()
<< "\nThe initial capacity of v is: " << v.capacity();
v.push_back( 2 ); // method push_back() is in
v.push_back( 3 ); // every sequence collection
v.push_back( 4 );
cout << "\nThe size of v is: " << v.size()
<< "\nThe capacity of v is: " << v.capacity();
cout << "\n\nContents of array a using pointer notation: ";
for ( int *ptr = a; ptr != a + SIZE; ++ptr )
cout << *ptr << ' ';
cout << "\nContents of vector v using iterator notation: ";
printVector( v );
cout << "\nReversed contents of vector v: ";
vector< int >::reverse_iterator p2;
for ( p2 = v.rbegin(); p2 != v.rend(); ++p2 )
cout << *p2 << ' ';
cout << endl;
return 0;
}
template < class T >
void printVector( const vector< T > &vec )
{
vector< T >::const_iterator p1;
for ( p1 = vec.begin(); p1 != vec.end(); p1++ )
cout << *p1 << ' ';
}
/*
Demonstrates
Vector declaration
passing vector to method via const reference
push_back
size
capacity
begin
end
rbegin
rend
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -