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

  • 相关阅读:
    使用ansible 批量分发ssh密钥
    修改DNS
    如何使用openssl生成RSA公钥和私钥对
    压力测试 php-fpm 优化
    mysql 安装
    svn 权限配置
    powerdesigner导出word
    Mysql无法创建外键的原因
    office project 激活
    MySQL日志恢复误删记录
  • 原文地址:https://www.cnblogs.com/shanguanghui/p/3595708.html
Copyright © 2011-2022 走看看