replace 的用法
的有关信息介绍如下:
replace是STL 算法中的一种,其用法如下:Examines each element in a range and replaces it if it matches a specified value.template void replace( ForwardIterator _First, ForwardIterator _Last, const Type& _OldVal, const Type& _NewVal );Parameters_FirstA forward iterator pointing to the position of the first element in the range from which elements are being replaced._LastA forward iterator pointing to the position one past the final element in the range from which elements are being replaced._OldValThe old value of the elements being replaced._NewValThe new value being assigned to the elements with the old value.RemarksThe range referenced must be valid; all pointers must be dereferenceable and within the sequence the last position is reachable from the first by incrementation.The order of the elements not replaced remains stable.The operator== used to determine the equality between elements must impose an equivalence relation between its operands.The complexity is linear; there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.Example Copy Code // alg_replace.cpp// compile with: /EHsc#include #include #include int main( ) { using namespace std; vector v1; vector ::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;}Sample Output Copy Code The original vector v1 is: ( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 ).The vector v1 with a value 700 replacing that of 7 is: ( 700 1 9 2 0 700 700 3 4 6 8 5 700 700 ).RequirementsHeader: Namespace: std为你简单的做下翻译:四个参数分别是:1.起始迭代器指向位置,如:v.begin();2.结束迭代器位置,3.旧的元素4.新的元素函数意义:将迭代器范围内的所有旧元素替换成新的元素。希望您问的是c++。
版权声明:文章由 问百问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.wenbwen.com/answer/214571.html