算法简介:
copy //容器内指定范围的元素拷贝到另一容器中
replace //将容器内指定范围的旧元素修改为新元素
repalce_if //容器内指定范围满足条件的元素替换为新元素
swap //互换两个容器元素
copy:
函数原型:copy(iterator beg, iterator end, iterator dest)
//按值查找元素 找到返回指定位置迭代器 找不到返回结束迭代器位置
//beg 开始迭代器
//end 结束迭代器
//dest 目标起始迭代器
#includeusing namespace std; #include #include //常用拷贝和替换算法 copy void myPrint(int val){ cout << val << " "; } void test01(){ vector v1; v1.push_back(50); v1.push_back(20); v1.push_back(10); v1.push_back(30); for_each(v1.begin(), v1.end(), myPrint); cout << endl; vector v2; //提前开辟空间 v2.resize(v1.size()); //开始拷贝 copy(v1.begin(), v1.end(), v2.begin()); for_each(v2.begin(), v2.end(), myPrint); cout << endl; } int main() { test01(); return 0; }
输出结果:
50 20 10 30
50 20 10 30
repalce:
功能: 将容器内指定范围的旧元素修改为新元素
函数原型:repalce(iterator beg, iterator end, oldvalue, newvalue);
//将区间内旧元素 替换成新元素
//beg 开始迭代器
//end 结束迭代器
//oldvalue 旧元素
//newvalue 新元素
#includeusing namespace std; #include #include //常用拷贝和替换算法 replace class MyPrint { public: void operator()(int val){ cout << val << " "; } }; void test01(){ vector v; v.push_back(50); v.push_back(20); v.push_back(10); v.push_back(30); v.push_back(20); //这里用仿函数的匿名对象作为最后一个参数输出 for_each(v.begin(), v.end(), MyPrint()); cout << endl; //把容器中所有的20替换成100 replace(v.begin(), v.end(), 20, 100); for_each(v.begin(), v.end(), MyPrint()); cout << endl; } int main() { test01(); return 0; }
输出结果:
50 20 10 30 20
50 100 10 30 100
replace_if:
功能: 容器内指定范围满足条件的元素替换为新元素
函数原型:replace_if(iterator beg, iterator end, _Pred, newvalue);
//按条件替换元素 满足条件的替换成指定元素
//beg 开始迭代器
//end 结束迭代器
//_Pred 谓词
//newvalue 替换的新元素
#includeusing namespace std; #include #include //常用的拷贝和替换算法 replace_if class MyPrint { public: void operator()(int val){ cout << val << " "; } }; class Greater30 { public: //谓词 bool operator()(int val){ return val >= 30; } }; void test01(){ vector v; v.push_back(10); v.push_back(30); v.push_back(40); v.push_back(100); v.push_back(20); v.push_back(30); v.push_back(15); cout << "替换前:" << endl; for_each(v.begin(), v.end(), MyPrint()); cout << endl; //将大于等于30的 替换为3000 第三个参数是一个函数匿名对象 replace_if(v.begin(), v.end(), Greater30(), 3000); cout << "替换后:" << endl; for_each(v.begin(), v.end(), MyPrint()); cout << endl; } int main(){ test01(); return 0; }
输出结果:
替换前:
10 30 40 100 20 30 15
替换后:
10 3000 3000 3000 20 3000 15
swap:
功能:互换两个容器的元素
函数原型:swap(container c1, container c2);
//c1 容器1
//c2 容器2
注意⚠️:连个互换的容器必须同种类型容器
#includeusing namespace std; #include #include //常用的拷贝和替换算法 swap class MyPrint { public: void operator()(int val){ cout << val << " "; } }; void test01(){ vector v1; vector 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; //交换 swap(v1, v2); cout << "交换后:" << endl; for_each(v1.begin(), v1.end(), MyPrint()); cout << endl; for_each(v2.begin(), v2.end(), MyPrint()); cout << endl; } int main(){ test01(); return 0; }
输出结果:
常用算数生成算法交换前:
0 1 2 3 4 5 6 7 8 9
100 101 102 103 104 105 106 107 108 109
交换后:
100 101 102 103 104 105 106 107 108 109
0 1 2 3 4 5 6 7 8 9
注意:算术生成算法属于小型算法 使用时包含的头文件为#include
算法简介:
accumulate //计算容器元素累计总和
fill //向容器中添加元素
accumulate:
功能:计算区间内 容器元素累加总和
函数原型:accumulate(iterator beg, iterator end, value);
//beg 开始迭代器
//end 结束迭代器
//value 起始值
#includeusing namespace std; #include #include //常用的算数生成算法 accumulate void test01(){ vector v; //插入数据0~100 for(int i=0; i<=100; i++){ v.push_back(i); } //这里计算的是0加到100 并加上初始值1 结果为5051 //初始值可以自己设定 int total = accumulate(v.begin(), v.end(), 1); cout <<"total = " << total << endl; } int main(){ test01(); return 0; }
输出结果:
total = 5051
fill:
功能:向容器中填充指定的元素 主要用于后期填充
函数原型:fill(iterator beg, iterator end, value);
//beg 开始迭代器
//end 结束迭代器
//value 填充的值
#includeusing namespace std; #include #include #include //常用的算数生成算法 fill class MyPrint { public: void operator()(int val){ cout << val << " "; } }; void test01(){ vector v; //指定大小为10 这里目前有默认的10个零 v.resize(10); //后期重新填充 //把默认的0都替换成100 fill(v.begin(), v.end(), 100); //遍历输出 for_each(v.begin(), v.end(), MyPrint()); cout << endl; } int main(){ test01(); return 0; }
输出结果:
常用集合算法100 100 100 100 100 100 100 100 100 100
算法简介:
set_intersection //求两个容器的交集
set_union //求两个容器的并集
set_difference //求两个容器的差集
set_intersection:
函数原型:set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
//beg1 源容器1开始迭代器
//end1 源容器1结束迭代器
//beg2 源容器2开始迭代器
//end2 源容器2结束迭代器
//dest 目标容器开始迭代器
注意⚠️:源容器必须是有序的序列
#includeusing namespace std; #include #include //常用的集合算法 set_intersection class MyPrint { public: void operator()(int val){ cout << val << " "; } }; void test01(){ vector v1; vector v2; //给v1和v2插数据 源容器必须是有序的序列 for(int i=0; i<10; i++){ v1.push_back(i); v2.push_back(i+5); } //创建目标容器 vector vTarget; //目标容器需要提前开辟空间 //这里我们取两个容器中大小较小的作为vTarget的容量 //因为交集不可能大于这个数 vTarget.resize(min(v1.size(), v2.size())); //获取交集 vector ::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); //itEnd接收的返回是Target容器中最后一个有效的交集数据的后一位 //所以这里我们直接使用itEnd //不然输出结果的末尾会补零 5 6 7 8 9 0 0 0 0 0 for_each(vTarget.begin(), itEnd, MyPrint()); cout << endl; } int main(){ test01(); return 0; }
输出结果:
5 6 7 8 9
set_union:
功能描述: 求两个集合的并集
函数原型:set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
//beg1 源容器1开始迭代器
//end1 源容器1结束迭代器
//beg2 源容器2开始迭代器
//end2 源容器2结束迭代器
//dest 目标容器开始迭代器
注意⚠️:两个源容器集合必须是有序的序列
#includeusing namespace std; #include #include //常用的集合算法 set_union class MyPrint { public: void operator()(int val){ cout << val << " "; } }; void test01(){ //创建源容器 vector v1; vector v2; //给v1和v2插数据 源容器必须是有序的序列 for(int i=0; i<10; i++){ v1.push_back(i); v2.push_back(i+5); } //创建目标容器 vector vTarget; //目标容器需要提前开辟空间 //这里我们取两个源容器的累加 //这种特殊情况出现在两个源容器没有任何交集 vTarget.resize(v1.size()+v2.size()); //获取并集 vector ::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); //itEnd接收的返回是Target容器中最后一个有效的并集数据的后一位 //所以这里我们直接使用itEnd //不然输出结果的末尾会补零 //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0 0 for_each(vTarget.begin(), itEnd, MyPrint()); cout << endl; } int main(){ test01(); return 0; }
输出结果:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
set_difference
功能描述:求两个集合的差集
函数原型:set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
//beg1 源容器1开始迭代器
//end1 源容器1结束迭代器
//beg2 源容器2开始迭代器
//end2 源容器2结束迭代器
//dest 目标容器开始迭代器
注意⚠️:两个源容器集合必须是有序的序列
v1和v2的差集:v1 - v1∩v2
v2和v1的差集: v2 - v1∩v2
#includeusing namespace std; #include #include //常用的集合算法 set_difference class MyPrint { public: void operator()(int val){ cout << val << " "; } }; void test01(){ //创建源容器 vector v1; vector v2; //给v1和v2插数据 源容器必须是有序的序列 for(int i=0; i<10; i++){ v1.push_back(i); v2.push_back(i+5); } //创建目标容器 vector vTarget; //目标容器需要提前开辟空间 //这里我们取两个源容器中size较大的容器 //这种特殊情况出现在两个源容器没有任何交集 vTarget.resize(max(v1.size(), v2.size())); //获取差集 vector ::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); //itEnd接收的返回是Target容器中最后一个有效的差集数据的后一位 //所以这里我们直接使用itEnd //不然输出结果的末尾会补零 //0 1 2 3 4 0 0 0 0 0 for_each(vTarget.begin(), itEnd, MyPrint()); cout << endl; } int main(){ test01(); return 0; }
输出结果:
0 1 2 3 4



