C++ STL常用算法之常用拷贝和替换算法

常用拷贝和替换算法

学习目标:

  • 掌握常用的拷贝和替换算法

算法简介:

  • copy // 容器内指定范围的元素拷贝到另一容器中

  • replace // 将容器内指定范围的旧元素修改为新元素

  • replace_if // 容器内指定范围满足条件的元素替换为新元素

  • swap // 互换两个容器的元素

copy

功能描述:

  • 容器内指定范围的元素拷贝到另一容器中

函数原型:

  • copy(iterator beg, iterator end, iterator dest);

    • beg:开始迭代器

    • end:结束迭代器

    • dest:目标起始迭代器

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i + 1);
    }
    cout << "V1:";
    for(auto it = v1.begin(); it != v1.end(); it++){
        cout << (*it) << " ";
    }
    cout << endl;
    vector<int> v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    //copy(v1.begin(), v1.end(), back_inserter(v2));
    cout << "V2:";
    for(auto it = v2.begin(); it != v2.end(); it++){
        cout << (*it) << " ";
    }
    cout << endl;
    cout << endl;
}

int main() {
    test01();

    return 0;
}

replace

功能描述:

  • 将容器内指定范围的旧元素修改为新元素

函数原型:

  • replace(iterator beg, iterator end, oldvalue, newvalue);

    • beg:开始迭代器

    • end:结束迭代器

    • oldvalue:旧元素

    • newvalue:新元素

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    // 创建一个包含重复元素的 vector
    vector<int> v = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};

    // 输出原始 vector 的内容
    cout << "原始 vector 的内容: ";
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    // 使用 replace 函数将所有的 2 替换为 20
    replace(v.begin(), v.end(), 2, 20);

    // 输出替换后的 vector 的内容
    cout << "替换后的 vector 的内容: ";
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    return 0;
}

replace_if

功能描述:

  • 将区间内满足条件的元素,替换成指定元素

函数原型:

  • replace_if(iterator beg, iterator end, _pred, newvalue);

    • beg:开始迭代器

    • end:结束迭代器

    • _pred:谓词(条件)

    • newvalue:替换的新元素

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


class isEven{

public:
    bool operator()(int val){
        return val % 2 == 0;
    }
};
int main() {
    // 创建一个包含多个元素的 vector
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // 输出原始 vector 的内容
    cout << "原始 vector 的内容: ";
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    // 使用 replace_if 函数将所有的偶数替换为 0
    replace_if(v.begin(), v.end(), isEven(), 0);

    // 输出替换后的 vector 的内容
    cout << "替换后的 vector 的内容: ";
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    return 0;
}

swap

功能描述:

  • 互换两个容器的元素

函数原型:

  • swap(container c1, container c2);

    • c1:容器 1

    • c2:容器 2

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    // 创建两个 vector 容器
    vector<int> v1 = {1, 2, 3, 4, 5};
    vector<int> v2 = {6, 7, 8, 9, 10};

    // 输出原始容器的内容
    cout << "交换前:" << endl;
    cout << "v1: ";
    for (auto it = v1.begin(); it != v1.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    cout << "v2: ";
    for (auto it = v2.begin(); it != v2.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    // 使用 swap 函数交换两个容器的内容
    swap(v1, v2);

    // 输出交换后的容器内容
    cout << "交换后:" << endl;
    cout << "v1: ";
    for (auto it = v1.begin(); it != v1.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    cout << "v2: ";
    for (auto it = v2.begin(); it != v2.end(); it++) {
        cout << (*it) << " ";
    }
    cout << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值