常用拷贝和替换算法

【简介】:

  • ​copy; // 容器内指定范围的元素拷贝到另一个容器中​
  • ​replace; // 将容器内指定范围的旧元素修改为新元素​
  • ​replace_if; // 按条件进行替换,容器内指定分为内满足条件的元素替换为新元素​
  • ​swap; // 互换另个容器中的元素​

copy

【功能】:

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

【函数原型】:

​copy(iterator begin,iterator end,iterator dest);​

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

// begin 开始迭代器

// end 结束迭代器

// dest 目标容器起始迭代器

【demo】:

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

using namespace std;

class MyPrint
{
public:
// 一元谓词
void operator()(int v)
{
cout << v << " ";
}
};
void test01()
{
vector<int> v1;
# 初始化
for (int i = 0; i < 5; i++)
{
v1.push_back(i);
}
vector<int> v2;
// 目标容器提前开辟空间
v2.resize(v1.size());
// 拷贝v1的元素到v2中
copy(v1.begin(), v1.end(), v2.begin());
// 使用for_each进行遍历操作
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
}

int main()
{

test01();
return 0;
}

replace

【功能】:

将容器内指定范围的旧元素替换为新元素

【函数原型】:

​replace(iterator begin,iterator end,oldvalue,newvalue);​

// 将区间内的旧元素替换为新元素

// begin 开始迭代器

// end 结束迭代器

// oldvalue 旧值

// newvalue 新值

【demo】:

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

using namespace std;

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

void test01()
{
vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);

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

cout << "将2替换成100后:";
// 开始迭代器,结束迭代器,旧值,新值
replace(v.begin(), v.end(), 2, 100);
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}

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

replace_if

【功能】:

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

【函数原型】:

​replace_if(iterator begin,iterator end,_Pred,newValue);​

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

// begin 开始迭代器

// end 结束迭代器

// _Pred 谓词(仿函数)

// newVlue 替换的新元素

【demo】:

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

using namespace std;

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

// 将大于3的数字替换掉
class ReplaceGreater3
{
public:
// 一元谓词
bool operator()(int v)
{
return v >= 3;
}
};

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

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

cout << "替换后:";
replace_if(v.begin(), v.end(), ReplaceGreater3(), 100);
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
return 0;
}

swap

【功能】:

互换两个容器中的元素

【函数原型】:

​swap(container c1,container c2);​

// 互换两个容器中的元素

// c1 容器1

// c2 容器2

【demo】:

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

using namespace std;

class MyPrint
{
public:
void operator()(int v)
{
cout << v << " ";
}
};
void test01()
{
vector<int> v1;
for (int i = 0; i < 5; i++)
{
v1.push_back(i);
}

vector<int> v2;
for (int i = 5; i < 10; i++)
{
v2.push_back(i);
}

cout << "交换前两个的容器元素:";
for_each(v1.begin(), v1.end(), MyPrint());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;

cout << "交换后两个容器的元素:";
swap(v1, v2);
for_each(v1.begin(), v1.end(), MyPrint());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
return 0;
}