📜  在C++中std :: 字符串:: replace,std :: 字符串:: replace_if

📅  最后修改于: 2021-05-30 07:11:06             🧑  作者: Mango

代替()

它用newValue替换范围[first,last)中等于oldValue的每个元素。该函数使用运算符==将各个元素与old_value进行比较。

template 
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);
first, last : the range of elements to process
old_value : the value of elements to replace
new_value : the value to use as replacement
// CPP code to demonstrate replace()
  
#include 
using namespace std;
  
// Function for demonstration
void replaceDemo(string s1, string s2, string s3, string s4)
{
    // Replaces 7 characters from 0th index by s2
    s1.replace(0, 7, s2);
    cout << s1 << endl;
  
    // Repalces 3 characters from 0th index with "Hello"
    s4.replace(0, 3, "Hello ");
    cout << s4 << endl;
  
    // Replaces 5 characters from 6th index of s4 with
    // 5 characters from 0th of s3
    s4.replace(6, 5, s3, 0, 5);
    cout << s4 << endl;
  
    // Replaces 5 charcaters from 6th index of s4 with
    // 6 characters from string "to all"
    s4.replace(6, 5, "to all", 6);
    cout << s4 << endl;
  
    // Replaces 1 character from 12th  index of s4 with
    // 3 copies of '!'
    s4.replace(12, 1, 3, '!');
    cout << s4 << endl;
}
  
// Driver code
int main()
{
    string s1 = "Example of replace";
    string s2 = "Demonstration";
    string s3 = "GeeksforGeeks";
    string s4 = "HeyWorld !";
  
    replaceDemo(s1, s2, s3, s4);
    return 0;
}

输出:

Demonstration of replace
Hello World !
Hello Geeks !
Hello to all !
Hello to all!!!!

replace_if

replace_if()会用newValue替换一元谓词op(elem)产生true的[first,last)范围内的每个元素。

template 
  void replace_if (ForwardIterator first, ForwardIterator last,
                   UnaryPredicate pred, const T& new_value );
Unary predicate are just functions that take a single parameter 
and return a Boolean value. It is used to check whether an 
element fits the criterion
// CPP code to demonstrate replace_if()
  
#include 
#include 
#include  // Header file for replace_if
using namespace std;
  
// Function to check if character is vowel or not
bool IsVowel(char ch)
{
    return (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
}
  
// Function to replace all vowels with 'V'
void replace_ifDemo(vector&v)
{
    replace_if(v.begin(), v.end(), IsVowel, 'V');
}
  
// Function to print content of vector
void print(vector&v)
{
    int len = v.size();
    for (int i = 0; i < len; i++)
        cout << v[i] << " ";
    cout << endl;
}
  
// Driver code
int main()
{
    vector v;
  
    for (int i = 0; i <= 6; i++)
        v.push_back('A' + i);
    cout << "Before replace_if " <<": "; 
    print(v);
    replace_ifDemo(v);
      
    cout << "After replace_if " << ": ";
    print(v);
      
    return 0;
}

输出:

Before replace_if : A B C D E F G 
After replace_if : V B C D V F G 

相关文章: std ::字符串:: replace_copy(),std ::字符串:: replace_copy_if

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”