1:迭代器类型:前向迭代器
2:返回值类型:void
3:算法功能:无op版本,遍历区间[beg,end)中每一个元素,遇到元素值为oldValue将其替换成newValue。有op版本,遍历区间[beg,end)中每一个元素调用op(elem),返回值为true时,将其替换成newValue
4:复杂度:线性复杂度
#include
#include
#include
using namespace std;
int main()
{
//把3替换成4
vector<int>c1 = {1,2,3,3,4,5,6,7,8,9};
cout << "c1:";
copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
replace(c1.begin(),c1.end(),3,4);
cout << "c1:";
copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
#include
#include
#include
using namespace std;
int main()
{
//将C1中大于等于5的元素替换成9
vector<int>c1 = {1,2,3,3,4,5,6,7,8,9};
cout << "c1:";
copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
replace_if(c1.begin(), c1.end(), [](int i)->bool { return i >= 5; }, 9);
cout << "c1:";
copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
1:迭代器类型:源区间----输入迭代器,目标区间—输出迭代器
2:返回类型:输出迭代器,两者均返回第一个未被覆盖的元素
3:算法功能:无op版本,遍历区间[beg,end)中每一个元素,遇到元素值为oldValue将其替换成newValue。有op版本,遍历区间[beg,end)中每一个元素调用op(elem),返回值为true时,将其替换成newValue----------替换结果复制到目标区间去
4:复杂度:线性复杂度
#include
#include
#include
using namespace std;
int main()
{
//将c1中值为3的元素替换成5,结果复制到目标区间c2中
vector<int>c1 = {1,2,3,3,4,5,6,7,8,9};
vector<int>c2;
cout << "c1:";
copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
//注意:当c2没有足够空间时我们要采用插入迭代器
replace_copy(c1.begin(), c1.end(),back_inserter(c2), 3, 4);
cout << "c2:";
copy(c2.begin(), c2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
#include
#include
#include
using namespace std;
int main()
{
//将c1中大于等于5的元素替换成9,结果复制到目标区间c2去
vector<int>c1 = {1,2,3,3,4,5,6,7,8,9};
vector<int>c2;
cout << "c1:";
copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
//注意:c2没有足够的空间时我们要使用插入迭代器
replace_copy_if(c1.begin(), c1.end(),back_inserter(c2), [](int i)->bool { return i >= 5; }, 9);
cout << "c2:";
copy(c2.begin(), c2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}