C++ 算法 中的 replace_copy函数

首页 / C++入门教程 / C++ 算法 中的 replace_copy函数

C++算法 replace_copy()函数用于复制参数[first,last),并将所有old_value替换为new_value值。它使用operator =进行复制并比较它使用operator ==的元素。

此函数检查源参数中的每个元素,如果将结果匹配到指定的值,则将其替换,然后将结果复制到新的目标参数中。

replace_copy - 语法

template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& old_value, const T& new_value);

replace_copy - 参数

first:一个输入迭代器,该迭代器指向要替换元素的参数内的初始位置。

last:一个输入迭代器,它指向要替换元素的参数内的最终位置。

result:一个输出迭代器,指向存储结果序列的参数的第一个元素。

old_value :要替换的元素的旧值。

new_value :分配给具有旧值的元素的新值。

replace_copy - 返回值

replace_copy()函数返回一个输出迭代器,该迭代器指向指向结果序列中最后写入的元素的位置。

replace_copy - 例子1

让我们看一个简单的示例来演示replace_copy()的用法:

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main() {
  vector<int> v = { 3,1,2,1,2 };
  
  replace_copy(v.begin(), v.end(),
    ostream_iterator<int>(cout, ","), 1, 10);
}

输出:

3,10,2,10,2,

replace_copy - 例子2

让我们看另一个简单的例子:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-replace-copy-function.html

来源:LearnFk无涯教程网

#include <iostream>    //std::cout
#include <algorithm>   //std::replace_copy
#include <vector>      //std::vector

using namespace std;

int main () {
  int myints[] = { 10, 20, 30, 15, 20, 10, 10, 20 };

  vector<int> myvector (8);
  replace_copy (myints, myints+8, myvector.begin(), 20, 99);

  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

输出:

myvector contains: 10 99 30 15 99 10 10 99

replace_copy - 例子3

让我们看另一个简单的例子:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-replace-copy-function.html

来源:LearnFk无涯教程网

#include <vector>  
#include <list>  
#include <algorithm>  
#include <iostream>  
  
int main( ) {  
   using namespace std;  
   vector <int> v1;  
   list <int> L1 (15);  
   vector <int>::iterator Iter1;  
   list <int>::iterator L_Iter1;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
      v1.push_back( i );  
  
   int ii;  
   for ( ii = 0 ; ii <= 3 ; ii++ )  
      v1.push_back( 7 );  
  
   random_shuffle ( v1.begin( ), v1.end( ) );  
  
   int iii;  
   for ( iii = 0 ; iii <= 15 ; iii++ )  
      v1.push_back( 1 );  
  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
  //Replace elements in one part of a vector with a value of 7  
  //with a value of 70 and copy into another part of the vector  
   replace_copy ( v1.begin( ), v1.begin( ) + 14,v1.end( ) -15, 7 , 70);  
  
   cout << "The vector v1 with a value 70 replacing that of 7 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
  //Replace elements in a vector with a value of 70  
  //with a value of 1 and copy into a list  
   replace_copy ( v1.begin( ), v1.begin( ) + 14,L1.begin( ), 7 , 1);  
  
   cout << "The list copy L1 of v1 with the value 0 replacing "  
        << "that of 7 is:\n ( " ;  
   for ( L_Iter1 = L1.begin( ) ; L_Iter1 != L1.end( ) ; L_Iter1++ )  
      cout << *L_Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}

输出:

The original vector v1 is:
 ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ).
The vector v1 with a value 70 replacing that of 7 is:
 ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 4 70 70 70 0 5 70 1 6 9 3 70 8 2 1 ).
The list copy L1 of v1 with the value 0 replacing that of 7 is:
 ( 4 1 1 1 0 5 1 1 6 9 3 1 8 2 0 ).

replace_copy - 例子4

让我们看另一个简单的例子:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-replace-copy-function.html

来源:LearnFk无涯教程网

#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 
  
// Function to replace 'A' at v.begin() with Z and copy it 
// to v.begin() position 
void replace_copyDemo(vector<char>& v) 
{ 
    replace_copy(v.begin(), v.begin()+1, v.begin(), 'A', 'Z' ); 
} 
  
// Function to print content of vector 
void print(vector<char>& v) 
{ 
    int len = v.size(); 
    for (int i = 0; i < len; i++) 
        cout << v[i] << " "; 
    cout << endl; 
} 
  
// Driver code 
int main() 
{ 
      vector<char> v; 
  
    for (int i = 0; i <= 6; i++) 
        v.push_back('A' + i); 
    cout << "Before replace_copy " <<": ";  
    print(v); 
    replace_copyDemo(v); 
      
    cout << "After replace_copy " << ": "; 
    print(v); 
  
    return 0; 
}

输出:

Before replace_copy : A B C D E F G 
After replace_copy : Z B C D E F G

在上面的示例中,函数replace_copy()用于将v.begin()处的" A"替换为Z,并将其复制到v.begin()位置。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

如何设计一个秒杀系统 -〔许令波〕

玩转Spring全家桶 -〔丁雪丰〕

现代C++编程实战 -〔吴咏炜〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

eBPF核心技术与实战 -〔倪朋飞〕

遗留系统现代化实战 -〔姚琪琳〕

深入拆解消息队列47讲 -〔许文强〕

AI大模型企业应用实战 -〔蔡超〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)