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

  • 相关阅读:
    1118诗名,诗词形式,类别实体导入
    1119飞花令句子,好友关系导入
    1116五言诗生成&古今地名标注与展示
    1120地点实体与事件实体导入
    1121实体导入总结
    1111诗人生平信息提取
    1114诗词收集&藏头诗生成&Snownlp正负情感分析
    1112全体诗人个人生平提取
    1113七言诗词收集与LSTM自动写诗
    WPF ListView DataGrid日期时间类型格式转换
  • 原文地址:https://www.cnblogs.com/shanguanghui/p/3595708.html
Copyright © 2011-2022 走看看