#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
namespace ClassFoo{
bool IfEqual(int m, int n) {
return (m == n);
}
void Unique_2() {
std::vector<int> BigVector1 = { 8, 23, 5, 5, 7, 7, 29, 0, 7, 7, 7, 1, 1 };
std::vector<int> BigVector2 = { 8, 23, 5, 5, 7, 7, 29, 0, 7, 7, 7, 1, 1 };
// 等价比较
auto it = std::unique(
std::begin(BigVector1),
std::end(BigVector1));
BigVector1.erase(it, BigVector1.end());
// 输出去重后的结果
std::copy(
BigVector1.begin(),
BigVector1.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// 自定谓词比较
it = std::unique(
std::begin(BigVector2),
std::end(BigVector2),
IfEqual);
BigVector2.erase(it, BigVector2.end());
// 输出去重后的结果
std::copy(
BigVector2.begin(),
BigVector2.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
}
int main()
{
ClassFoo::Unique_2();
return 0;
}
通过使用unique达到去重的效果,通过copy进行复制,然后通过写入标准输出流进行输出
C++ copy,unique,ostream_iterator用法
最新推荐文章于 2022-11-24 17:35:23 发布