• 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

  • 相关阅读:
    php array function
    scrum敏捷开发重点介绍
    PHP文件操作
    正则
    PHP面向对象
    PHP数组
    PHP函数参数
    PHP运算符优先级
    PHP判断变量类型和类型转换的三种方式
    PHP变量的传值和引用
  • 原文地址:https://www.cnblogs.com/shanguanghui/p/3595708.html
Copyright © 2020-2023  润新知