Loading... ## 算法简介: copy;//容器内指定范围的元素拷贝到另一容器中 replace;//将容器内指定范围的旧元素改为新元素 replace_if;//容器内指定范围满足条件的元素替换为新元素 swap;//互换两个容器的元素 ## copy ### 功能描述: 容器内指定范围的元素拷贝到另一容器中 ### 函数原型: copy(iterator beg, iterator end, iterator dest) //按值查找元素,找到返回指定位置迭代器,找不到返回迭代器的位置 //beg 开始迭代器 //end 结束迭代器 //dest 目标起始迭代器 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> using namespace std; class print { public: void operator()(const int val) { cout << val << " "; } }; void text1()//内置数据类型 { vector<int>v; vector<int>v1; for (int i = 0; i < 10; i++) { v.push_back(i); } cout << "拷贝前v:" << endl; for_each(v.begin(), v.end(), print()); cout <<endl<< "拷贝后v1:" << endl; v1.resize(v.size()); copy(v.begin(), v.end(), v1.begin()); for_each(v1.begin(), v1.end(), print()); } int main() { text1(); return 0; } ``` ## replace ### 功能描述: 将容器内指定范围的旧元素修改为新元素 ### 函数原型: replace(iterator beg, iterator end, oldvalue, newvalue); //将区间内旧元素替换成新元素 //beg开始迭代器 //end结束迭代器 //oldvalue旧元素 //newvalue新元素 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> using namespace std; class print { public: void operator()(const int val) { cout << val << " "; } }; void text1()//内置数据类型 { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } cout << "替换前v:" << endl; for_each(v.begin(), v.end(), print()); cout <<endl<< "替换后:" << endl; replace(v.begin(), v.end(), 3,100);//将3替换成100 for_each(v.begin(), v.end(), print()); } int main() { text1(); return 0; } ``` ## replace_if ### 功能描述: 将区间内满足条件的元素,替换成指定元素 ### 函数原型: replace_if(iterator beg, iterator end, _pred, newvalue) //按条件替换元素,满足条件的替换成指定元素 //beg 开始迭代器 //end 结束迭代器 //_pred 谓词 //newvalue 替换的新元素 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> using namespace std; class print { public: void operator()(const int val) { cout << val << " "; } }; class replace_six { public: bool operator()(const int val) { return val > 6; } }; void text1()//内置数据类型 { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } cout << "替换前v:" << endl; for_each(v.begin(), v.end(), print()); cout <<endl<< "替换后:" << endl; replace_if(v.begin(), v.end(), replace_six(),100);//将大于6的元素替换成100 for_each(v.begin(), v.end(), print()); } int main() { text1(); return 0; } ``` ## swap ### 功能描述: 互换两个容器的元素 ### 函数原型: swap(container c1, container c2); //互换两个容器的元素 //c1容器1 //c2容器2 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> using namespace std; class print { public: void operator()(const int val) { cout << val << " "; } }; void text1()//内置数据类型 { vector<int>v; vector<int>v1; for (int i = 0; i < 10; i++) { v.push_back(i); } for (int i = 10; i > 0; i--) { v1.push_back(i); } cout << "交换前:" << endl; cout << "v: "; for_each(v.begin(), v.end(), print()); cout << endl << "v1: "; for_each(v1.begin(), v1.end(), print()); cout << endl<<"交换后:" << endl; swap(v,v1); cout << "v: "; for_each(v.begin(), v.end(), print()); cout << endl << "v1: "; for_each(v1.begin(), v1.end(), print()); } int main() { text1(); return 0; } ``` 最后修改:2021 年 09 月 24 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。