转接自STL算法
1. remove() 将区间中等于value的元素移到区间后面(后面元素向前填充)
2. remove_if() remove()满足谓词的元素
3. remove_copy() 将区间的元素value移除copy到另一序列
4. remove_copy_if() 将区间满足谓词的元素remove_copy()
5. unique() 将相邻满足谓词元素移除一个
6. unique_copy() 将相邻元素满足谓词元素删除后序列copy到另一序列
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
// 1. remove() 将区间中等于value的元素移到区间后面(后面元素向前填充)
// 2. remove_if() remove()满足谓词的元素
// 3. remove_copy() 将区间的元素value移除copy到另一序列
// 4. remove_copy_if() 将区间满足谓词的元素remove_copy()
// 5. unique() 将相邻满足谓词元素移除一个
// 6. unique_copy() 将相邻元素满足谓词元素删除后序列copy到另一序列
void test()
{
//1
vector<int> a{ 1,2,3,4,5,6,7,5,5};
auto pos = remove(a.begin(), a.end(), 5);
a.erase(pos, a.end());
for (auto &v : a)
cout << v << ends;
cout << endl;
//2
vector<int> b{ 1,2,3,4,5,6,7,8,9,0 };
auto f = [](const int& v)
{
return v % 2 == 0;
};
auto ip = remove_if(b.begin(), b.end(), f);
b.erase(ip, b.end());
for (auto &v : b)
cout << v << ends;
cout << endl;
//3
vector<int> c;
remove_copy(a.begin(), a.end(), back_inserter(c), 1);
for (auto &v : c)
cout << v << ends;
cout << endl;
//4
vector<int> d{ 1,2,3,4,5,6,7,8,9,0 };
vector<int> e;
remove_copy_if(d.begin(), d.end(), back_inserter(e), f);
for (auto &v : e)
cout << v << ends;
cout << endl;
//5
vector<int> g{ 1,2,3,4,6,7,8 };
auto fp = [](const int& a, const int& b)
{
return b - a == 2;
};
auto gp = unique(g.begin(), g.end(), fp);
g.erase(gp, g.end());
for (auto &v : g)
cout << v << ends;
cout << endl;
//6
vector<int> m{ 1,2,3,4,5,7,9,11 };
vector<int> n;
unique_copy(m.begin(), m.end(), back_inserter(n), fp);
for (auto &v : n)
cout << v << ends;
}
int main()
{
test();
system("pause");
return 0;
}