📜  C++中的std :: replace和std :: replace_if

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

std :: replace

将new_value分配给与old_value比较的[first,last)范围内的所有元素。该函数使用运算符==将各个元素与old_value进行比较

函数模板:

void replace (ForwardIterator first, 
              ForwardIterator last,
              const T& old_value,
              const T& new_value)
first, last : 
Forward iterators to the initial and final positions
in a sequence of elelments.
old_value : Value to be replaced.
new_value : Replacement value.

返回值:
此函数不返回任何值。如果找到需要替换的元素,则元素
更换,否则保持不变。

例子:

Input : 10 20 30 30 20 10 10 20 
Output : 10 99 30 30 99 10 10 99 
// Replaced value 20 in vector to 99.

Input : 3 5 7 8 9 5 4 
Output : 3 5 7 12 9 5 4  
// Replaced value 8 by 12.
CPP
// CPP program to find and replace the value
// with another value in array
// using std::replace
#include 
using namespace std;
 
// Driver code
int main()
{
    int arr[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // variable containing the old and new values
    int old_val = 20, new_val = 99;
 
    // print old array
    cout << "Original Array:";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << '\n';
 
    // Function used to replace the values
    replace(arr, arr + n, old_val, new_val);
 
    // new array after using std::replace
    cout << "New Array:";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << '\n';
 
    return 0;
}


CPP
// CPP code to find all the elements that are odd
// and replace them with 0.
// using std::replace_if
#include 
using namespace std;
 
// Function that is used in std::replace_if
// If number is odd return 1, else 0
// 1 (True) means replace the number
// 0 (False) means does not replace
bool IsOdd(int i)
{
  return ((i % 2) == 1);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // print old array
    cout << "Original Array:";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << '\n';
 
    // replacement value
    int new_val = 0;
 
    // replace_if function
    replace_if(arr, arr + n, IsOdd, new_val);
 
    // new array after using std::replace
    cout << "New Array:";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << '\n';
 
    return 0;
}


输出
Original Array: 10 20 30 30 20 10 10 20
New Array: 10 99 30 30 99 10 10 99

std :: replace_if

将new_value分配给pred返回true的[first,last)范围内的所有元素。
函数模板:

void replace_if (ForwardIterator first, ForwardIterator last,
                   UnaryPredicate pred, const T& new_value)
first, last : Forward iterators to the initial and final positions
in a sequence of elelments.
pred : Unary function that accepts an element in the range as argument, and
returns a value convertible to bool.The returned value indicate whether
the element is to be replaced (if true, it is replaced).
The function shall not modify its argument.
old_value : Value to be replaced.
new_value : Replacement value. 

例子:

Input : 1 2 3 4 5 6 7 8 9 10  
Output : 0 2 0 4 0 6 0 8 0 10  
// Replaced all odd values to 0.

Input : 10 20 30 30 20 10 10 20 
Output : 10 4 30 30 4 10 10 4  
// Replaced all number divisible by 4 to 4.

CPP

// CPP code to find all the elements that are odd
// and replace them with 0.
// using std::replace_if
#include 
using namespace std;
 
// Function that is used in std::replace_if
// If number is odd return 1, else 0
// 1 (True) means replace the number
// 0 (False) means does not replace
bool IsOdd(int i)
{
  return ((i % 2) == 1);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // print old array
    cout << "Original Array:";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << '\n';
 
    // replacement value
    int new_val = 0;
 
    // replace_if function
    replace_if(arr, arr + n, IsOdd, new_val);
 
    // new array after using std::replace
    cout << "New Array:";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << '\n';
 
    return 0;
}
输出
Original Array: 1 2 3 4 5 6 7 8 9 10
New Array: 0 2 0 4 0 6 0 8 0 10
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”