unique原型:
equality (1) | template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
|
---|---|
predicate (2) | template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred); |
删除指定范围中的所有连续重复元素,仅仅留下每组等值元素中的第一个元素。
该函数不会实际上删除容器内的元素。而是通过使用下一个元素替换重复元素来完成删除操作。
返回值被认为是新的容器超尾迭代器(但不是end())。
行为类似于:
template <class ForwardIterator> ForwardIterator unique (ForwardIterator first, ForwardIterator last) { if (first==last) return last; ForwardIterator result = first; while (++first != last) { if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2) *(++result)=*first; } return ++result; }一个简单的例子:
// unique algorithm example #include <iostream> // std::cout #include <algorithm> // std::unique, std::distance #include <vector> // std::vector bool myfunction (int i, int j) { return (i==j); } void munique () { int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10 std::vector<int> myvector (myints,myints+9); // using default comparison: std::vector<int>::iterator it; it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^ std::cout<<"myvector="; for (auto i=myvector.begin(); i!=myvector.end(); ++i) std::cout << ' ' << *i; std::cout << '\n'; myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10 // using predicate comparison: std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; }运行截图:
unique_copy原型:
equality (1) | template <class InputIterator, class OutputIterator> OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result); |
---|---|
predicate (2) | template <class InputIterator, class OutputIterator, class BinaryPredicate> OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); |
行为类似于:
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result)
{
if (first==last) return result;
*result = *first;
while (++first != last) {
typename iterator_traits<InputIterator>::value_type val = *first;
if (!(*result == val)) // or: if (!pred(*result,val)) for version (2)
*(++result)=val;
}
return ++result;
}
一个简单的例子:
// unique_copy example #include <iostream> // std::cout #include <algorithm> // std::unique_copy, std::sort, std::distance #include <vector> // std::vector void uniquecopy() { int myints[] = {10,20,20,20,30,30,20,20,10}; std::vector<int> myvector (9); // 0 0 0 0 0 0 0 0 0 // using default comparison: std::vector<int>::iterator it; it=std::unique_copy (myints,myints+9,myvector.begin()); // 10 20 30 20 10 0 0 0 0 // ^ //std::sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0 // ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; }运行结果:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:[email protected]
2014-9-26
于GDUT
——————————————————————————————————————————————————