unique()函数头文件#include
作用为保留字符串/容器/数组中的不重复数列(去重);
调用:将重复元素放到末尾,会更改原数列
返回值:去重后数列的尾地址
用法:
#include #include#include #include #include using namespace std; string a = {'b', 'a', 'b', 'c', 'd', 'd', 'e'}; vector b = {2, 1, 2, 3, 4, 4, 5}; int c[7] = {2, 1, 2, 3, 4, 4, 5}; int main() { sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c, c + 7); cout << (unique(a.begin(), a.end()) - a.begin()) << endl; for (auto i : a) cout << i << " "; cout << endl; cout << (unique(b.begin(), b.end()) - b.begin()) << endl; for (auto i : b) cout << i << " "; cout << endl; cout << (unique(c, c + 7) - c) << endl; for (auto i : c) cout << i << " "; cout << endl; }
运行结果:
注意:
使用unique()函数前要排序
此时数组c没有进行排序:



