STL常用代码

// display3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <algorithm>
#include<ostream>
#include<iostream>
#include <vector>
#include<functional>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	
    vector<int> vTest;

	//减少重复分配
	vTest.reserve(10);
	for(int i=0;i<10;++i)
	    vTest.push_back(rand()%20);

	//去除多分配的空间
	vector<int>(vTest).swap(vTest);

	//输出显示
	copy(vTest.begin(),vTest.end(),ostream_iterator<int>(cout," "));
	cout<<endl;
    
    //返回算法迭代器
	vector<int>::iterator it;
	                                         //二元函数的使用
    //it=partition(vTest.begin(),vTest.end(),bind2nd(greater_equal<int>(),4) );
	 it=unique(vTest.begin(),vTest.end());
	
	copy(vTest.begin(),vTest.end(),ostream_iterator<int>(cout," "));
	cout<<endl;
    
	//利用迭代器删除元素
	vTest.erase(it,vTest.end());
    copy(vTest.begin(),vTest.end(),ostream_iterator<int>(cout," "));
	cout<<endl;



	getchar();

	return 0;
}

猜你喜欢

转载自blog.csdn.net/sichuanpb/article/details/80909758