zoukankan      html  css  js  c++  java
  • replace() replace_copy()

    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

  • 相关阅读:
    微软 软件的 一组堆成快捷键
    C#事件 的讲解
    软件缺陷分析的几种方法
    一个长三角人对深圳的看法 (转)
    一次LoadRunner的CPC考试经历
    测试是一门武功
    ORACLE的性能测试经验总结
    深圳测试协会第九次论坛在深圳举行
    10月28日参加了IBM的产品推介会
    什么是web安全性测试?
  • 原文地址:https://www.cnblogs.com/shanguanghui/p/3595708.html
Copyright © 2011-2022 走看看