#include <iostream>
#include <vector>
using namespace std;
int main()
{
string s;
vector<string> s1;
int i=5;
/*
往容器里面存放元素
*/
while( i>0 )
{
cin >> s;
s1.push_back(s);
--i;
}
cout << s1.size() << endl;
/*
声明迭代器
*/
vector<string>::iterator it;
for( it=s1.begin(); it!=s1.end(); ++it)
{
cout << *it << " ";
}
/*
往容器里面插入元素
*/
s1.insert(s1.begin(), "......");
cout << endl;
for( it=s1.begin(); it!=s1.end(); ++it)
{
cout << *it << " ";
}
cout << endl << s1.at(1);
/*
删除尾端元素
*/
s1.pop_back();
s1.pop_back();
s1.pop_back();
cout << endl;
for( it=s1.begin(); it!=s1.end(); ++it)
{
cout << *it << " ";
}
/*
删除某个迭代器指定的元素
*/
it=s1.begin();
s1.erase(it+2);
cout << endl;
for( it=s1.begin(); it!=s1.end(); ++it)
{
cout << *it << " ";
}
/*
重新设置容器大小
*/
s1.resize(20, "wang");
cout << endl;
for( it=s1.begin(); it!=s1.end(); ++it)
{
cout << *it << " ";
}
/*
傻瓜循环,删除偶数元素,复制每个奇数元素
*/
vector<int> vi;
for(int i=0; i<10; ++i)
{
vi.push_back(i);
}
vector<int>::iterator iter = vi.begin();
while( iter != vi.end() )
{
if( *iter %2 )
{
iter = vi.insert(iter, *iter);
iter += 2;
}
else
{
iter = vi.erase(iter);
}
}
cout << endl;
for( iter=vi.begin(); iter != vi.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
vector<int>::iterator begin = vi.begin();
while( begin != vi.end() )
{
++begin;
begin = vi.insert(begin, 42);
++begin;
}
cout << endl;
for( iter=vi.begin(); iter != vi.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
/*
capacity和size
*/
vector<int> ivec;
cout << endl;
cout << "ivec: size: " << ivec.size()
<< " capacity: " << ivec.capacity() <<endl;
for( vector<int>::size_type ix = 0; ix != 24; ++ix)
{
ivec.push_back(ix);
}
cout << "ivec: size: " << ivec.size()
<< " capacity: " << ivec.capacity() <<endl;
ivec.reserve(50);
cout << "ivec: size: " << ivec.size()
<< " capacity: " << ivec.capacity() <<endl;
while( ivec.size() != ivec.capacity() )
{
ivec.push_back(0);
}
cout << "ivec: size: " << ivec.size()
<< " capacity: " << ivec.capacity() <<endl;
ivec.push_back(44);
cout << "ivec: size: " << ivec.size()
<< " capacity: " << ivec.capacity() <<endl << endl;
vector<int>::iterator v;
v = ivec.begin();
while( v!= ivec.end() )
{
cout << *v << " ";
++v;
}
cin >> i;
return 0;
}