int a[] = {1,2,3,3,4};
vector<int> v(a, a+5);
vector<int> v2;
//replace(v.begin(), v.end(), 3, 9); //把 v 中的3 替换为 9
replace_copy(v.begin(), v.end(), back_inserter(v2), 3, 9); //把 v 中的3 替换为 9 赋值给v2 ,v的值不变
for (int i =0; i<v.size(); i++)
{
cout<<v[i]<<endl;
}
for (int i=0; i<v2.size(); i++)
{
cout<<v2[i]<<endl;
}
*********************************************
int ia[] = { 1, 2, 3, 4, 100, 5, 100 };
vector< int > iVec( ia, ia+7 );
cout << "
The contents of iVec: ";
for ( vector<int>::iterator it = iVec.begin(); it != iVec.end(); ++it )
{
cout << *it << " ";
}
cout << endl;
list<int> iLst;
// copy iVec's member to iLst;
cout << "
Using inserter: " << endl;
replace_copy( iVec.begin(), iVec.end(), inserter( iLst, iLst.begin() ), 100, 0 );
cout << " The contents of iLst: ";
for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout << endl;
cout << "
Using back_inserter: " << endl;
iLst.clear();
replace_copy( iVec.begin(), iVec.end(), back_inserter( iLst ), 100, 0 );
cout << " The contents of iLst: ";
for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout << endl;
cout << "
Using front_inserter: " << endl;
iLst.clear();
replace_copy( iVec.begin(), iVec.end(), front_inserter( iLst ), 100, 0 );
cout << " The contents of iLst: ";
for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout<<endl;
replace(iLst.begin(), iLst.end(), 0 , 8);
for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
{
cout << *it << " ";
}
cout << endl;
system("pause");
********************************************************
原文:http://wenwen.soso.com/z/q150702174.htm