📜  std ::字符串:: replace_copy(),std ::字符串:: replace_copy_if在C++中

📅  最后修改于: 2021-05-30 12:43:52             🧑  作者: Mango

replace_copy

replace_copy()是copy()和replace()的组合。

  • 它将[first,last)范围内的元素复制到以result开始的范围内,用new_value替换old_value的外观。
  • 复制的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但last指向的元素。
  • 范围不应以结果指向范围[first,last)内的元素的方式重叠。
  • 该函数使用运算符==将各个元素与old_value进行比较。
template 
  OutputIterator replace_copy (InputIterator first, InputIterator last,
                               OutputIterator result,
                               const T& old_value, const T& new_value); 
first, last : Input iterators to the initial and final positions 
in a sequence. 
old_value : Value to be replaced.
new_value : Replacement value.

Returns the position after the last copied element in the destination
range (the first element that is not overwritten). 
CPP
// CPP to illustrate
// replace_copy
 
#include 
#include 
#include 
using namespace std;
 
// Function to replace 'A' at v.begin() with Z and copy it
// to v.begin() position
void replace_copyDemo(vector& v)
{
    replace_copy(v.begin(), v.begin()+1,
                    v.begin(), 'A', 'Z' );
}
 
// 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_copy " <<": ";
    print(v);
    replace_copyDemo(v);
     
    cout << "After replace_copy " << ": ";
    print(v);
 
    return 0;
}


CPP
// CPP code to illustrate
// replace_copy_if
#include 
#include 
#include 
using namespace std;
 
// Function to check if number is even
int IsEven(int i)
{
    return ((i % 2) == 0);
}
 
// 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;
}
 
// Function to replace all
// even numbers from vector v1 and
// copying them to v2
void replace_copy_ifDemo(vector& v1,
                            vector& v2)
{
    replace_copy_if(v1.begin(), v1.end(),
                       v2.begin(), IsEven, 0);
}
 
// Driver Code
int main()
{
    vector v1, v2;
 
    for (int i = 1; i <= 10; i++)
        v1.push_back(i);
 
    cout << "Before replace_copy_if : ";
    print(v1);
 
    v2.resize(v1.size()); // allocate space
    replace_copy_ifDemo(v1, v2);
 
    cout << "After replace_copy_if : ";
    print(v2);
 
    return 0;
}


输出:

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

replace_copy_if

replace_copy_if()是copy()和replace_if()的组合。

  • 将[first,last)范围内的元素复制到以result开始的范围内,用new_value替换pred返回true的那些元素。
  • 简单来说,它将一个序列的元素复制到另一个相同大小的序列,用另一个值替换满足谓词的任何元素。
  • 返回目标范围中最后一个复制的元素之后的位置(第一个未被覆盖的元素)。
Template 
  OutputIterator replace_copy_if (InputIterator first, InputIterator last,
                                  OutputIterator result, UnaryPredicate pred,
                                  const T& new_value);
first, last : Input iterators to the initial and final positions 
in a sequence. 
old_value : Value to be replaced.
new_value : Replacement value.
pred : Unary function that accepts an element in the range as argument, 
and returns a value convertible to bool. The value returned indicates whether the 
element is to be replaced in the copy (if true, it is replaced).

CPP

// CPP code to illustrate
// replace_copy_if
#include 
#include 
#include 
using namespace std;
 
// Function to check if number is even
int IsEven(int i)
{
    return ((i % 2) == 0);
}
 
// 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;
}
 
// Function to replace all
// even numbers from vector v1 and
// copying them to v2
void replace_copy_ifDemo(vector& v1,
                            vector& v2)
{
    replace_copy_if(v1.begin(), v1.end(),
                       v2.begin(), IsEven, 0);
}
 
// Driver Code
int main()
{
    vector v1, v2;
 
    for (int i = 1; i <= 10; i++)
        v1.push_back(i);
 
    cout << "Before replace_copy_if : ";
    print(v1);
 
    v2.resize(v1.size()); // allocate space
    replace_copy_ifDemo(v1, v2);
 
    cout << "After replace_copy_if : ";
    print(v2);
 
    return 0;
}

输出:

Before replace_copy_if : 1 2 3 4 5 6 7 8 9 10 
After replace_copy_if : 1 0 3 0 5 0 7 0 9 0 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”