C++ //常用拷贝和替换算法 //copy //replace 将指定区间范围内的旧元素修改为新元素 //replace_if(满足条件的元素,替换指定的元素) //swap 互换两个容器的元素

//常用拷贝和替换算法
//copy
//replace 将指定区间范围内的旧元素修改为新元素
//replace_if(满足条件的元素,替换指定的元素)
//swap 互换两个容器的元素
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

void test01()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    vector<int>v2;
    v2.resize(v.size());
    copy(v.begin(), v.end(), v2.begin());
    for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    v2 = v;
    for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

}

//repalce
class MyPrint
{
public:
    void operator()(int val)
    {
        cout<< val << " ";
    }
};

void  Myprint(int v)
{
    cout<< v<<" ";
}
void test02()
{
    vector<int>v2;
    v2.push_back(20);
    v2.push_back(50);
    v2.push_back(30);
    v2.push_back(60);
    v2.push_back(20);
    v2.push_back(20);
    v2.push_back(90);
    v2.push_back(20);
    v2.push_back(20);

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

    for_each(v2.begin(), v2.end(), MyPrint());
    cout << endl;



    //将20替换999(所有的20都替换)
    replace(v2.begin(), v2.end(), 20, 999);
    cout << "替换后" << endl;
    for_each(v2.begin(), v2.end(), MyPrint());
    cout << endl;


}

//replace_if
void  Mythirty(int t)
{
    cout << t << " ";
}

class Great30
{
public:
    int operator()(int val)
    {
        return val >= 30;
    }
};

int chongzai(int a)
{
    return a >= 30;
}

void test03()
{
    vector<int>v3;
    v3.push_back(50);
    v3.push_back(600);
    v3.push_back(55);
    v3.push_back(89);
    v3.push_back(20);
    v3.push_back(30);
    v3.push_back(70);
    v3.push_back(0);
    v3.push_back(36);
    v3.push_back(60);
    cout << "没换前:" << endl;
    for_each(v3.begin(), v3.end(), Mythirty);
    cout << endl;
    //将大于 30 的元素换成 789
    cout << "换了后:" << endl;
    replace_if(v3.begin(), v3.end(), Great30(), 789);

    for_each(v3.begin(), v3.end(), Mythirty);
    cout << endl;

    cout << "换了后:" << endl;
    replace_if(v3.begin(), v3.end(), chongzai, 789);

    for_each(v3.begin(), v3.end(), Mythirty);
    cout << endl;

}

//swap
void test04()
{
    vector<int>v4;
    vector<int>v5;

    v4.push_back(10);
    v4.push_back(20);
    v4.push_back(30);
    v4.push_back(40);
    v4.push_back(50);


    v5.push_back(100);
    v5.push_back(200);
    v5.push_back(300);
    v5.push_back(400);
    v5.push_back(500);

    cout << "交换前:" << endl;
    cout << "V4 =    " << endl;
    for (vector<int>::iterator it = v4.begin(); it != v4.end(); it++)
    {
        
        cout << *it << " ";
    }
    cout << endl;

    cout << "V5 =    " << endl;
    for (vector<int>::iterator it = v5.begin(); it != v5.end(); it++)
    {
        
        cout << *it << " ";
    }
    cout << endl;

    
    swap(v5,v4);

    cout << "交换后:" << endl;
    cout << "V4 =    " << endl;
    for (vector<int>::iterator it = v4.begin(); it != v4.end(); it++)
    {
        cout<< *it << " ";
    }
    cout << endl;

    cout << "V5  =    " << endl;
    for (vector<int>::iterator it = v5.begin(); it != v5.end(); it++)
    {
        cout  << *it << " ";
    }
    cout << endl;


}
int main()
{
    test01();
    test02();
    test03();
    
    test04();
    system("pause");
    return 0;
}

 

posted on 2021-08-18 19:21  Bytezero!  阅读(104)  评论(0编辑  收藏  举报