C++类属性unique

简介: 类属性算法unique的作用是从输入序列中去掉所有相邻的重复元素。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7  using namespace std; 8 9  int mai...

类属性算法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

相关文章
|
3天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
28天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
37 0
|
28天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
33 0
|
3天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
2天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
6天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
8天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”