早教吧 育儿知识 作业答案 考试题库 百科 知识分享

简明扼要的介绍下replacecopy()函数的用法.

题目详情
简明扼要的介绍下replace_copy()函数的用法.
▼优质解答
答案和解析
是STL里面的replace_copy()吗?如果是,那么这个函数的意思就是把一个区间(range)的数据拷贝到另一个区间里;而且copy的同时,作replace(替换).


比如:
#include      
#include     
#include        

using namespace std;

int main () {
  int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
  vector myvector (8);
  
  replace_copy (myints, myints+8, myvector.begin(), 20, 99);
  for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';
  
  return 0;
}
输出结果是:
10 99 30 30 99 10 10 99
就是把myints中的数据copy到myvector中,同时把myints中所有值=20的元素替换为99.