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

算法简介:

  • copy // 容器内指定的元素拷贝到另一容器
  • replace // 将容器内指定范围的旧元素改为新元素
  • replace_if // 容器内指定范围满足条件的元素替换为新元素
  • swap //互换两个容器的元素

1. copy

功能描述:

复制代码
将容器内指定范围的数据拷贝到另一容器中

函数原型:

复制代码
copy(iterator beg, iterator end, iterator pos);
beg 开始迭代器
end 结束迭代器
pos 插入数据的位置

// main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

void test01() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int> v2;
    v2.resize(v.size());
    copy(v.begin(), v.end(), v2.begin());

    // 或者继续使用lambda表达式,确保编译器支持C++11及以上标准
    for_each(v2.begin(), v2.end(), [](int val) {
        cout << val << " ";
    });
    cout << endl;
}

int main() {
    test01();
    return 0;
}

2. replace

功能描述:

复制代码
 将容器内指定范围的元素替换为新元素

函数原型:

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

参数说明:

复制代码
beg  开始迭代器
end  结束迭代器
oldvalue  旧元素
newvalue  新元素
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

// replace


void myPrint(int val){
    cout << val << " ";
}

void test01()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);
    cout << "替换前:" << endl;
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    cout << "替换后:" << endl;
    // 将容器中的20替换为2000
    replace(v.begin(), v.end(), 20, 2000);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}


int main(int argc, char const *argv[])
{
    test01();
    return 0;
}

3. replace_if

功能描述:

复制代码
将容器中满足条件的元素替换为新的元素

函数原型:

复制代码
replace(iterator beg, iterator end, _pred, newvalue);

参数说明:

复制代码
beg 开始迭代器
end 结束迭代器
_pred 谓词
newvalue 替换的新元素
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

// replace_if


void myPrint(int val){
    cout << val << " ";
}

class MyCompare{
public:
    MyCompare(int num) : num(num){}
    bool operator()(int val){
        return val >= 20;
    }
    int num;
};

void test01()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);
    cout << "替换前:" << endl;
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    cout << "替换后:" << endl;
    // 将容器中的20替换为2000 ,可以用deepseek 查看bind2nd 含义,或者看源码
    replace_if(v.begin(), v.end(), bind2nd(greater_equal<int>(), 20), 2000);
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    cout << "自定义防函数替换:" << endl;
    replace_if(v.begin(), v.end(), MyCompare(20), 60);
    for_each(v.begin(), v.end(), myPrint);
}


int main(int argc, char const *argv[])
{
    test01();
    return 0;
}

4. swap

功能:

复制代码
交换两个容器的元素, 需要是同一类型的容器

函数原型:

复制代码
swap(v1, v2);
参数说明:
v1 容器1
v2 容器2
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

// swap


void myPrint(int val){
    cout << val << " ";
}

class MyCompare{
public:
    MyCompare(int num) : num(num){}
    bool operator()(int val){
        return val >= 20;
    }
    int num;
};

void test01()
{
    vector<int> v1;
    vector<int>v2;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i+100);
    }
    cout << "交换前" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;

    cout << "交换后" << endl;
    swap(v1, v2);
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;

}


int main(int argc, char const *argv[])
{
    test01();
    return 0;
}
相关推荐
小刘同学32111 分钟前
C++11 特性
c++·c11新特性
军训猫猫头11 分钟前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
真的想上岸啊25 分钟前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
m0_5522008229 分钟前
《UE5_C++多人TPS完整教程》学习笔记40 ——《P41 装备(武器)姿势(Equipped Pose)》
c++·游戏·ue5
明天好,会的32 分钟前
跨平台ZeroMQ:在Rust中使用zmq库的完整指南
开发语言·后端·rust
丁劲犇1 小时前
用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
开发语言·c++·qt·tui·字符界面·curse
旷世奇才李先生1 小时前
Next.js 安装使用教程
开发语言·javascript·ecmascript
charlie1145141912 小时前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
呜喵王阿尔萨斯2 小时前
编程中的英语
c语言·c++
likeGhee3 小时前
python缓存装饰器实现方案
开发语言·python·缓存