孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

类属性算法unique的作用是从输入序列中去掉所有相邻的重复元素。

1 #include <iostream>
2 #include <cassert>
3 #include <algorithm>
4 #include <vector>
5 #include <string>
6 #include <iterator>
7  using namespace std;
8
9  int main()
10 {
11 cout<<"Illustrating the generic unique algorithm."<<endl;
12 const int N=11;
13 int array1[N]={1,2,0,3,3,0,7,7,7,0,8};
14 vector<int> vector1;
15 for (int i=0;i<N;++i)
16 vector1.push_back(array1[i]);
17
18 vector<int>::iterator new_end;
19 new_end=unique(vector1.begin(),vector1.end());
20 assert(vector1.size()==N);
21
22 vector1.erase(new_end,vector1.end());
23 copy(vector1.begin(),vector1.end(),ostream_iterator<int>(cout," "));
24 cout<<endl;
25
26 return 0;
27 }

输出结果为

Illustrating the generic unique algorithm.
1 2 0 3 0 7 0 8

posted on 2011-05-27 20:51  孤独的猫  阅读(795)  评论(0编辑  收藏  举报