随笔 - 322  文章 - 0  评论 - 4  阅读 - 77146

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   Bytezero!  阅读(123)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示