C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ replace()

C++ 算法replace()

C++ 算法 replace()函数用于将范围为[]的所有new_value值替换为old_value的所有值。首先,last)。
此函数检查范围内的每个元素,如果与指定值匹配,则将其替换。

语法

template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);

参数

first: 前向迭代器,指向替换元素范围内的初始位置。
last: 指向要替换元素范围内最终位置的前向迭代器。
old_value : 元素的旧值
new_value : 将新值分配给具有旧值的元素。

返回值

复杂度

复杂度在首尾之间的距离是线性的。

数据竞争

[first1,last1)范围内的对象将被访问并可能被修改。
>

异常安全

如果任何函数调用分配或对迭代器的操作引发异常,则引发异常。
请注意,无效参数引起不确定的行为。

示例1

让我们看一个简单的示例来演示replace()的用法:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> v = { 3,1,2,1,2 };
  replace(v.begin(), v.end(), 1, 10);
  for_each(v.begin(), v.end(),
    [](int x) { cout << x << ","; });
    
    return 0;
}
输出:
3,10,2,10,2,
在上面的示例中, Vector v的元素1被替换为10、

示例2

让我们看另一个简单的示例:
#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main( ) {  
   using namespace std;  
   vector <int> v1;  
   vector <int>::iterator 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( ) );  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Replace elements with a value of 7 with a value of 700  
   replace (v1.begin( ), v1.end( ), 7 , 700);  
  
   cout << "The vector v1 with a value 700 replacing that of 7 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}  
输出:
The original vector v1 is:
 ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
The vector v1 with a value 700 replacing that of 7 is:
 ( 4 700 700 700 0 5 700 1 6 9 3 700 8 2 ).
在上面的示例中,replace()从 Vector v1中找到所有与7匹配的元素,并将其替换为700。

示例3

我们来看另一个简单的例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
void print(vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
}
 
int main() {
    vector<int> v = {1, 4, 3, 2, 3, 10, 7, 9, 3, 8};
 
    cout << "v : ";
    print(v);
    // replace 3 with 6
    replace(v.begin(), v.end(), 3, 6);
    cout << "After replacing 3 with 6\n";
    cout << "v : ";
    print(v);
    
    return 0;
}
输出:
v : 1 4 3 2 3 10 7 9 3 8 
After replacing 3 with 6
v : 1 4 6 2 6 10 7 9 6 8

示例4

让我们看另一个简单的示例:
#include <iostream>     // cout
#include <algorithm>    // replace
#include <vector>       // vector
using namespace std;
int main () {
  int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
  vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20
  replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 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 30 99 10 10 99

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4