![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<stdio.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<vector>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<iostream>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<algorithm>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<functional>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
bool cmp(const int &a,const int &b)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return a>b;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
vector<int> v1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v1.push_back( 0 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v1.push_back( 1 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v1.push_back( 2 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v1.push_back( 3 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
vector<int> v2;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v2.push_back( 5 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v2.push_back( 6 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v2.push_back( 7 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
v2.push_back( 8 );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << "Before, v2 is: ";
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
for( vector<int>::size_type i = 0; i < v2.size(); i++ )
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << v2[i] << " ";
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
/**//* cout << "Before, v2 is: ";
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for( vector<int>::iterator iter =v2.begin() ; iter < v2.end(); iter++ ) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << *iter << " ";
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
cout << endl;//迭代器,相当于指针的概念。。*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v2.insert( v2.end(), v1.begin(), v1.end() );//在v2末尾插入v1
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v2.insert(v2.end(),3,'3'); //在v2末尾插入3个51
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// swap(v1,v2);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v1.swap(v2); //交换
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// printf("%d\n",v2.at(2)); // 输出指定位置值
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// sort(v2.begin(),v2.end(),cmp); //排序,一直没弄明白cmp
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// sort(v2.begin(),v2.end(),greater<int>()); //递减排序
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// sort(v2.begin(),v2.end(),less<int>()); //递增排序
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v2.assign(v1.begin(),v1.end());//拷贝v1到v2
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v2.erase(v2.end()-1,v2.end()); //删除容器元素,不包括第一个数
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v2.clear(); //清空
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// v2.resize(2); //修改元素个数
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << "After, v2 is: ";
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
for( vector<int>::size_type j = 0; j < v2.size(); j++ )
![](http://www.cppblog.com/Images/dot.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cout << v2[j] << " ";
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2010-09-03 13:52
jince 阅读(191)
评论(0) 编辑 收藏 引用 所属分类:
C++学习