继续学习STL算法,本文主要讲:
remove:用来“删除”从begin到end之间值为x的元素。例子中是“删除”从begin到end之间值为10的元素。确切的说remove翻译为移动比较好,因为这个函数不会改变vector中的元素,也不会破坏“删除”的元素,而是把所有没有“删除”的元素挪到了vector的前端。这个函数返回一个迭代器,指向vector中最后一个没有删除的元素后面的位置。
remove_if:“删除”那些满足条件的元素,需要有一个一元谓词函数。
remove_copy:例子中就是先去做移除10这个元素的操作,然后复制copy到c.begin()的位置。
remove_copy_if:“删除”那些满足条件的元素,再做拷贝。例子中是先移除所有大于9的元素,然后拷贝到c2.begin()的位置。
源代码如下:
// Fig22_28_STL_Alg_remove_remove_if_remove_copy_remove_copy_if.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
///Standard Library functions remove ,remove_if,remove_copy,remove_copy_if
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
bool greater9(int);//prototype
int main()
{
//std::cout << "Hello World!\n";
const int SIZE = 10;
int a[SIZE] = { 10,2,10,4,16,6,14,8,12,10 };
ostream_iterator<int> output(cout," ");
vector<int> v(a,a+SIZE);//copy of a
vector<int>::iterator newLastElement;
cout << "Vector v before removing all 10s:\n";
copy(v.begin(), v.end(),output);
//remove all 10s from v
newLastElement = remove(v.begin(),v.end(),10);
cout << "\n Vector v after removing all 10s:\n";
copy(v.begin(), newLastElement, output);
cout << "\n";
copy(v.begin(),v.end(), output);//后面的元素就不一定是什么了
vector<int> v2(a,a+SIZE);//copy of a
vector<int> c(SIZE,0);//实例化 vector c
cout << "\nVector v2 before removing all 10s and copying:\n";
copy(v2.begin(), v2.end(), output);
//copy from v2 to c,removing 10s in the process
remove_copy(v2.begin(), v2.end(),c.begin(),10);//先是把begin----end之间的10给remove了,然后复制到c中
cout << "\nVector c after removing all 10 s from v2:\n";
copy(c.begin(), c.end(), output);
vector<int> v3(a, a + SIZE);//copy of a
cout << "\nVector v3 before removing all elements greater than 9:\n";
copy(v3.begin(), v3.end(), output);
//removing elements greater than 9 from v3
newLastElement = remove_if(v3.begin(),v3.end(),greater9);
cout << "\nVector v3 after removing all elements greater than 9:\n";
copy(v3.begin(), newLastElement, output);
cout << "\n";
copy(v3.begin(), v3.end(), output);
///////////////////
vector<int> v4(a, a + SIZE);//copy of a
vector<int> c2(SIZE, 0);//实例化 vector c2
cout << "\nVector v4 before removing all elements greater than 9 and copying:\n";
copy(v4.begin(), v4.end(), output);
//copy elements from v4 to c2,removing elements greater than 9 in the process
remove_copy_if(v4.begin(),v4.end(),c2.begin(),greater9);
cout << "\nVector c2 after removing all elements greater than 9 and copying:\n";
copy(c2.begin(), c2.end(), output);
cout << endl;
}
//determine weather argument is greater than 9
bool greater9(int x)
{
return x > 9;
}
运行结果:
Vector v before removing all 10s:
10 2 10 4 16 6 14 8 12 10
Vector v after removing all 10s:
2 4 16 6 14 8 12
2 4 16 6 14 8 12 8 12 10
Vector v2 before removing all 10s and copying:
10 2 10 4 16 6 14 8 12 10
Vector c after removing all 10 s from v2:
2 4 16 6 14 8 12 0 0 0
Vector v3 before removing all elements greater than 9:
10 2 10 4 16 6 14 8 12 10
Vector v3 after removing all elements greater than 9:
2 4 6 8
2 4 6 8 16 6 14 8 12 10
Vector v4 before removing all elements greater than 9 and copying:
10 2 10 4 16 6 14 8 12 10
Vector c2 after removing all elements greater than 9 and copying:
2 4 6 8 0 0 0 0 0 0