所有容器适用
replace(b,e,ov,nv) //把oldvalue替换成newvalue
replace_if(b,e,p,v) //把符合p条件的替换成v
replace_copy(b1,e1,b2,ov,nv)
replace_copy_if(b1,e1,b2,p,v)
- /**------http://blog.csdn.net/u010579068------**/
- #include<iostream>
- #include<cstdio>
- #include<string>
- #include<vector>
- #include<list>
- #include<deque>
- #include<algorithm>
- using namespace std;
- /*****************************************
- //所有容器适用
- replace(b,e,ov,nv) //把oldvalue替换成newvalue
- replace_if(b,e,p,v) //把符合p条件的替换成v
- replace_copy(b1,e1,b2,ov,nv)
- replace_copy_if(b1,e1,b2,p,v)
- *****************************************/
- /**----------------------------------------------------------------------------------
- ----------------------------------------------------------------------------------**/
- /*************************************************************************************
- std::replace 所有排序容器适用 algorithm
- --------------------------------------------------------------------------------------
- template < class ForwardIterator, class T >
- void replace ( ForwardIterator first, ForwardIterator last,
- const T& old_value, const T& new_value );
- //eg:
- template < class ForwardIterator, class T >
- void replace ( ForwardIterator first, ForwardIterator last,
- const T& old_value, const T& new_value )
- {
- for (; first != last; ++first)
- if (*first == old_value) *first=new_value;
- }
- *************************************************************************************/
- /*************************************************************************************
- std::replace_if 所有排序容器适用 algorithm
- --------------------------------------------------------------------------------------
- template < class ForwardIterator, class Predicate, class T >
- void replace_if ( ForwardIterator first, ForwardIterator last,
- Predicate pred, const T& new_value );
- //eg:
- template < class ForwardIterator, class Predicate, class T >
- void replace_if ( ForwardIterator first, ForwardIterator last,
- Predicate pred, const T& new_value )
- {
- for (; first != last; ++first)
- if (pred(*first)) *first=new_value;
- }
- *************************************************************************************/
- /*************************************************************************************
- std::replace_copy 所有排序容器适用 algorithm
- --------------------------------------------------------------------------------------
- template < class InputIterator, class OutputIterator, class T >
- OutputIterator replace_copy ( InputIterator first, InputIterator last,
- OutputIterator result,
- const T& old_value, const T& new_value );
- //eg:
- template < class InputIterator, class OutputIterator, class T >
- OutputIterator replace_copy ( InputIterator first, InputIterator last,
- OutputIterator result, const T& old_value, const T& new_value )
- {
- for (; first != last; ++first, ++result)
- *result = (*first==old_value)? new_value: *first;
- return result;
- }
- *************************************************************************************/
- /*************************************************************************************
- std::replace_copy_if 所有排序容器适用 algorithm
- --------------------------------------------------------------------------------------
- template < class InputIterator, class OutputIterator, class Predicate, class T >
- OutputIterator replace_copy_if ( InputIterator first, InputIterator last,
- OutputIterator result, Predicate pred,
- const T& new_value );
- //eg:
- template < class InputIterator, class OutputIterator, class Predicate, class T >
- OutputIterator replace_copy_if ( InputIterator first, InputIterator last,
- OutputIterator result, Predicate pred,
- const T& new_value )
- {
- for (; first != last; ++first, ++result)
- *result = (pred(*first))? new_value: *first;
- return result;
- }
- *************************************************************************************/
- int main()
- {
- int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
- vector<int> myvector (myints, myints+8); // 10 20 30 30 20 10 10 20
- //replace(b,e,ov,nv)
- replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99
- cout << "myvector contains:";
- for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
- cout << " " << *it;
- cout << endl;
- /**--------------------------------------------------------------------------------**/
- myvector.clear();
- vector<int>::iterator it;
- // set some values:
- for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
- replace_if (myvector.begin(), myvector.end(), bind2nd(modulus<int>(),2), 0); // 0 2 0 4 0 6 0 8 0
- cout << "myvector contains:";
- for (it=myvector.begin(); it!=myvector.end(); ++it)
- cout << " " << *it;
- cout << endl;
- /**--------------------------------------------------------------------------------**/
- // int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
- list<int> mylist (8);
- replace_copy (myints, myints+8, mylist.begin(), 20, 66);
- cout << "mylist contains: ";
- for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
- cout << " " << *it;
- cout << endl;
- /**--------------------------------------------------------------------------------**/
- deque<int> first,second;
- deque<int>::iterator id;
- // set some values:
- for (int i=1; i<10; i++) first.push_back(i); // 1 2 3 4 5 6 7 8 9
- second.resize(first.size()); // allocate space
- replace_copy_if (first.begin(), first.end(), second.begin(), not1(bind2nd(modulus<int>(),2)), 0);
- // 1 0 3 0 5 0 7 0 9
- cout << "second contains: ";
- for (id=second.begin(); id!=second.end(); ++id)
- cout << " " << *id;
- cout << endl;
- return 0;
- }