美文网首页
14.replace替换算法

14.replace替换算法

作者: lxr_ | 来源:发表于2021-05-14 09:24 被阅读0次
#include<iostream>
using namespace std;

#include<vector>
#include<algorithm>

//replace(iterator begin, iterator end, oldValue, newValue);//将容器中所有oldValue替换为newValue


class MyPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};
void test1401()
{
    vector<int> v;

    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    v.push_back(4);

    cout << "替换前:" << endl;
    for_each(v.begin(), v.end(), MyPrint());
    cout << endl;

    replace(v.begin(), v.end(), 4, 10);

    cout << "替换后:" << endl;
    for_each(v.begin(), v.end(), MyPrint());
    cout << endl;
}

int main()
{
    test1401();

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:14.replace替换算法

      本文链接:https://www.haomeiwen.com/subject/ztgirltx.html