C++ 关于unique的用法

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;

bool same_elements(vector<int>a, vector<int> b)
{
sort(a.begin(),a.end());
sort(b.begin(),b.end());
return a == b;
}

void remove_duplicates(vector<int> a)
{
sort(a.begin(), a.end());
a.unique(); // 问题在这里, 用下面的方法就可以 问什么呢
// vector<int>::iterator it = unique(a.begin(), a.end());
// a.erase(it, a.end());
}

int main()
{
int a[9] = {1,4,9,16,9,7,4,9,11};
int b[9] = {11,1,4,9,16,9,7,4,9};

vector<int> va(a, a+9); // create vector va from array a
vector<int> vb(b, b+9); // create vector vb from array b

// check whether va and vb have same elements
cout << "The vectors va and vb have ";
if (!same_elements(va, vb)) cout << "not ";
cout << "the same elements.\n";

// remove duplicate elements from va
remove_duplicates(va);

copy(va.begin(), va.end(), ostream_iterator<int> (cout, " "));
system("pause");
return 0;
}
还有 请问unique 会返回什么值?

一.unique函数类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素。
该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值范围得结束。
// sort words alphabetically so we can find the duplicates
sort(words.begin(), words.end());
/* eliminate duplicate words:
* unique reorders words so that each word appears once in the
* front portion of words and returns an iterator one past the
unique range;
* erase uses a vector operation to remove the nonunique elements
*/
vector<string>::iterator end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
若调用sort后,vector的对象的元素按次序排列如下:
sort jumps over quick red red slow the the turtle
注意,words的大小并没有改变,依然保存着10个元素;只是这些元素的顺序改变了。调用unique“删除”了相邻的重复值。给“删除”加上引号是因为unique实际上并没有删除任何元素,而是将无重复的元素复制到序列的前段,从而覆盖相邻的重复元素。unique返回的迭代器指向超出无重复的元素范围末端的下一个位置。
注意:算法不直接修改容器的大小。如果需要添加或删除元素,则必须使用容器操作。
example:

#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
int main()
{
//cout<<"Illustrating the generic unique algorithm."<<endl;
const int N=11;
int array1[N]={1,2,0,3,3,0,7,7,7,0,8};
vector<int> vector1;
for (int i=0;i<N;++i)
vector1.push_back(array1[i]);
vector<int>::iterator new_end;
new_end=unique(vector1.begin(),vector1.end()); //"删除"相邻的重复元素
assert(vector1.size()==N);
vector1.erase(new_end,vector1.end()); //删除(真正的删除)重复的元素
copy(vector1.begin(),vector1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
return 0;
}



二、unique_copy函数
算法标准库定义了一个名为unique_copy的函数,其操作类似于unique。
唯一的区别在于:前者接受第三个迭代器实参,用于指定复制不重复元素的目标序列。
unique_copy根据字面意思就是去除重复元素再执行copy运算。
编写程序使用unique_copy将一个list对象中不重复的元素赋值到一个空的vector对象中。

//使用unique_copy算法
//将一个list对象中不重复的元素赋值到一个空的vector对象中
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int ia[7] = {5 , 2 , 2 , 2 , 100 , 5 , 2};
list<int> ilst(ia , ia + 7);
vector<int> ivec;//将list对象ilst中不重复的元素复制到空的vector对象ivec中//sort(ilst.begin() , ilst.end()); //不能用此种排序,
ilst.sort(); //在进行复制之前要先排序
unique_copy(ilst.begin() , ilst.end() , back_inserter(ivec));
//输出vector容器
cout<<"vector: "<<endl;
for(vector<int>::iterator iter = ivec.begin() ; iter != ivec.end() ; ++iter)
cout<<*iter<<" ";
cout<<endl;
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-05-16
一.unique函数类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素。
该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值范围得结束。
//
sort
words
alphabetically
so
we
can
find
the
duplicates
sort(words.begin(),
words.end());
/*
eliminate
duplicate
words:
*
unique
reorders
words
so
that
each
word
appears
once
in
the
*
front
portion
of
words
and
returns
an
iterator
one
past
the
unique
range;
*
erase
uses
a
vector
operation
to
remove
the
nonunique
elements
*/
vector<string>::iterator
end_unique
=
unique(words.begin(),
words.end());
words.erase(end_unique,
words.end());
在STL中unique函数是一个去重函数,
unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后
返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
若调用sort后,vector的对象的元素按次序排列如下:
sort
jumps
over
quick
red
red
slow
the
the
turtle
注意,words的大小并没有改变,依然保存着10个元素;只是这些元素的顺序改变了。调用unique“删除”了相邻的重复值。给“删除”加上引号是因为unique实际上并没有删除任何元素,而是将无重复的元素复制到序列的前段,从而覆盖相邻的重复元素。unique返回的迭代器指向超出无重复的元素范围末端的下一个位置。
注意:算法不直接修改容器的大小。如果需要添加或删除元素,则必须使用容器操作。
example:
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
int main()
{
//cout<<"Illustrating the generic unique algorithm."<<endl;
const int N=11;
int array1[N]={1,2,0,3,3,0,7,7,7,0,8};
vector<int> vector1;
for (int i=0;i<N;++i)
vector1.push_back(array1[i]);
vector<int>::iterator new_end;
new_end=unique(vector1.begin(),vector1.end()); //"删除"相邻的重复元素
assert(vector1.size()==N);
vector1.erase(new_end,vector1.end()); //删除(真正的删除)重复的元素
copy(vector1.begin(),vector1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
return 0;
}
二、unique_copy函数
算法标准库定义了一个名为unique_copy的函数,其操作类似于unique。
唯一的区别在于:前者接受第三个迭代器实参,用于指定复制不重复元素的目标序列。
unique_copy根据字面意思就是去除重复元素再执行copy运算。
编写程序使用unique_copy将一个list对象中不重复的元素赋值到一个空的vector对象中。
//使用unique_copy算法
//将一个list对象中不重复的元素赋值到一个空的vector对象中
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int ia[7] = {5 , 2 , 2 , 2 , 100 , 5 , 2};
list<int> ilst(ia , ia + 7);
vector<int> ivec;//将list对象ilst中不重复的元素复制到空的vector对象ivec中//sort(ilst.begin() , ilst.end()); //不能用此种排序,
ilst.sort(); //在进行复制之前要先排序
unique_copy(ilst.begin() , ilst.end() , back_inserter(ivec));
//输出vector容器
cout<<"vector: "<<endl;
for(vector<int>::iterator iter = ivec.begin() ; iter != ivec.end() ; ++iter)
cout<<*iter<<" ";
cout<<endl;
return 0;
}
第2个回答  2010-05-27
看一看介绍STL的书
相似回答