赋予新值
赋予完全相同的数值
void fill(ForwardIterator beg,ForwardIterator end,const T&newValue)
void fill_n(OutputIterator beg,Size num,const T&newValue)
- fill()将区间[begin,end)内的每一个元素都赋予新值newValue
- fill_n()将”从beg开始的前num个元素“都赋予新值newValue
- 必须保证目标区间有足够空间,否则就要使用插入型迭代器
#include"algostuff.h"
using namespace std;
int main()
{
fill_n(ostream_iterator<float>(cout, " "), 10, 0.6);
cout << endl;
list<string> col1;
//insert hello 9 tomes
fill_n(back_inserter(col1), 9, "hello");
PRINT_ELEMENTS(col1, "col1: ");
//overwrite all elements with "again"
fill(col1.begin(), col1.end(), "again");
PRINT_ELEMENTS(col1, "col1: ");
//replace "again" by "hi"
fill_n(col1.begin(), col1.size() - 3, "hi");
PRINT_ELEMENTS(col1, "col1: ");
list<string>::iterator pos1, pos2;
pos1 = col1.begin();
pos2 = col1.end();
fill(++pos1, --pos2, "hmmm");
PRINT_ELEMENTS(col1, "col1: ");
}
第一次调用展示了如何使用fill_n()打印特定数量的值,其他的操作则是展示了怎样在string list中安插和替换元素
赋予新产生的值
void generate(ForwardIterator beg,ForwardIterator end,Func op)
void generate_n(OutputIterator beg,Size num,Func op)
- generate()会调用op()操作产生新的值,并将其赋给区间[begin,end)内的每一个元素
- generate_n()会调用op()产生新值,并赋给以”beg为起始的区间“内的前num个操作
#include"algostuff.h"
using namespace std;
int main()
{
list<int> col1;
//insert five random numbers
generate_n(back_inserter(col1), 5, rand);
PRINT_ELEMENTS(col1, "col1: ");
//overwrite with five new random numbers
generate(col1.begin(), col1.end(), rand);
PRINT_ELEMENTS(col1, "col1: ");
}
替换元素
替换序列内的元素
void replace(ForwardIterator beg,ForwardIterator end,const T& oldValue,const T& newValue)
void replace_if(ForwardIterator beg,ForwardIterator end,UnaryPredicate op,const T& newValue)
- replace()将区间[beg,end)内每一个等于”oldValue"的元素替换为“newValue"
- replace_if()将区间[beg,end)内每一个令一元判断式op(elem)为true的元素替换为newValue
- op不应该在函数调用该过程中改变自身状态
#include"algostuff.h"
using namespace std;
int main()
{
list<int> col1,col2;
INSERT_ELEMENTS(col1, 2, 7);
INSERT_ELEMENTS(col1, 4, 9);
col2 = col1;
PRINT_ELEMENTS(col1, "col1: ");
//replace all value 6 with 42
replace(col1.begin(), col1.end(), 6, 42);
PRINT_ELEMENTS(col1, "after replace col1: ");
PRINT_ELEMENTS(col2, "col2(=col1): ");
//repalce with value less than 5 with 0
replace_if(col1.begin(), col1.end(), bind2nd(less<int>(), 5),0);
PRINT_ELEMENTS(col1, "col1: ");
}
复制并替换元素
OutputIterator replace_copy(InputIterator sourceBeg,InputIterator sourceEnd,
OutputIterator destBeg,
const T& oldValue,const T& newValue)
OutputIterator replace_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,
OutputIterator destBeg,
UnaryPredicate op,const T& newValue)
- replace_copy()是copy()和replace()的组合。它将源区间[beg,end)中的元素复制到以”destBeg为起点“的目标区间去,同时将其中”与oldValue相等“的所有元素替换为newValue
- replace_copy_if()是copy()和replace_if()的组合。它将源区间[beg,end)中的元素复制到以”destBeg为起点“的目标区间去,同时将其中”令一元判断式op(elem)结果为true“的所有元素替换为newValue
- 两个算法都返回目标区间中“最后一个被复制元素”的下一位置,也就是第一个未被覆盖的元素位置
- op不应该在函数调用过程中改变自身状态
#include"algostuff.h"
using namespace std;
int main()
{
list<int> col1;
INSERT_ELEMENTS(col1, 2, 6);
INSERT_ELEMENTS(col1, 4, 9);
PRINT_ELEMENTS(col1, "col1: ");
//print all elements with value 5 repalced with 55
replace_copy(col1.begin(), col1.end(), ostream_iterator<int>(cout, " "), 5, 55);
cout << endl;
//print all elements with a value less 5 repalced with 42
replace_copy_if(col1.begin(), col1.end(), ostream_iterator<int>(cout, " "), bind2nd(less<int>(), 5), 42);
cout << endl;
//print each elem while each odd element is replaced with 0
replace_copy_if(col1.begin(), col1.end(), ostream_iterator<int>(cout, " "), bind2nd(modulus<int>(), 2), 0);
cout << endl;
}
程序输出结果如下: