C++ replace_copy()
C++ 算法replace_copy()
C++ 算法 replace_copy()函数用于复制范围[first,last)并替换所有old_value由值new_value放入其中。它使用operator =进行复制并比较使用operator ==的元素。
此函数检查源范围中的每个元素,如果结果与指定值匹配,则将其替换,然后将结果复制到新的目标范围。
语法
template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& old_value, const T& new_value);
参数
first: 一个输入迭代器,指向要替换元素的范围内的初始位置。
last: 一个输入迭代器,它指向要替换元素的范围内的最终位置。
result: 一个输出迭代器,它指向
old_value : 要替换的元素的旧值。
new_value : 分配给具有旧值的元素的新值。
返回值
replace_copy()函数返回一个输出迭代器,该迭代器指向指向到结果序列中写入的last元素。
复杂度
复杂度在第一个和last距离内是线性的,并对每个元素执行比较和赋值
数据竞争
对象i n范围[first1,last1)被访问。
结果范围内的对象和返回值被修改。
异常安全性
如果元素比较,元素分配或迭代器上的任何操作引发异常,则此函数将引发异常。
请注意,无效参数会导致未定义的行为。
示例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);
}
输出:
示例2
让我们看另一个简单的示例:
#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
示例3
让我们看另一个简单的示例:
#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 ).
示例4
让我们看另一个简单的示例:
#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()位置。