【C++】容器元素的复制和变换

分类: 【C++】   772人阅读  评论(0)  收藏  举报

一、复制容器元素:copy()算法

copy()的原形如下:

[cpp]  view plain copy
  1. template<class InputIterator, class OutputIterator>  
  2.     OutputIterator copy(  
  3.     InputIterator _First, //源容器起始位置  
  4.     InputIterator _Last, //源容器终止位置  
  5.     OutputIterator _DestBeg //目标容器的起始位置  
  6.     );  
列子:将两张成绩表统计到一起,形成一张成绩总表。

[cpp]  view plain copy
  1. vector<int> vecScore1;  
  2. vector<int> vecScore2;  
  3. //对容器进行操作,保存成绩  
  4. ...  
  5. //保存总成绩的容器  
  6. vector<int> vecScore;  
  7. //根据各个容器的大小,重新设定总容器的容量  
  8. vecScore.resize(vecScore1.size() + vecScore2.size());  
  9. //复制第一张成绩单 itLast将指向所有复制进来的数据的末尾位置  
  10. vector<int>::iterator itLast = copy(vecScore1.begin(), vecScore1.end(), vecScore.begin());  
  11. //复制第二张  
  12. copy(vecScore2.begin(), vecScore2.end(), itLast);  


二、逆向复制容器元素:copy_backward()
copy()函数可以将某个容器中的数据正向的复制给另外一个容器,但有时候我们需要复制从后向前放置到目标容器中,这时候可以使用copy_backward()

原形:

[cpp]  view plain copy
  1. template<class BidirectionalIterator1, class BidirectionalIterator2>  
  2.    BidirectionalIterator2 copy_backward(  
  3.       BidirectionalIterator1 _First,   
  4.       BidirectionalIterator1 _Last,  
  5.       BidirectionalIterator2 _DestEnd //指向目标容器某个位置的迭代器,即从这个位置逐个向前放置到目标容器中  
  6.    );  
例如: 高校扩招2倍

[cpp]  view plain copy
  1. vector<Student> vecStudent;  
  2. //对容器进行操作,保存成绩  
  3. ...  
  4. //扩大两倍容器  
  5. vecStudent.resize(vecStudent.size() * 2);  
  6. copy_backward(vecStudent.begin(), vecStudent.begin() + vecStudent.size() / 2, vecStudent.end());  

三、合并容器元素 merge()

用以将两个源容器中的数据合并到目标容器的算法。

原型:

[html]  view plain copy
  1. template<class InputIterator1, class InputIterator2, class OutputIterator>  
  2.    OutputIterator merge(  
  3.       InputIterator1 _First1,   
  4.       InputIterator1 _Last1,  
  5.       InputIterator2 _First2,   
  6.       InputIterator2 _Last2,   
  7.       OutputIterator _Result  
  8.    );  
注意:使用merge()算法之前必须先使用sort()算法对两个源容器中的数据进行排序

[cpp]  view plain copy
  1. vector<int> vecScore1;  
  2. vector<int> vecScore2;  
  3. vector<int> vecScore;  
  4. //操作数据  
  5. ...  
  6. sort(vecScore1.begin(), vecScore1.end());  
  7. sort(vecScore2.begin(), vecScore2.end());  
  8. // 调整目标容器的大小  
  9. vecScore.resize(vecScore1.size() + vecScore2.size());  
  10. //合并到目标目标容器  
  11. merge(vecScore1.begin(), vecScore1.end(),  
  12.          vecScore2.begin(), vecScore2.end(),  
  13.          vecScore.begin());  
四、合并并去除冗余元素容器 set_union()

使用merge() 合并容器时,如果两个合并的容器中有相同的元素,则在合并后的容器中会出想两份相同的数据,有时候这个是不需要的。

[cpp]  view plain copy
  1. // 总清单  
  2. vector<string> vecGoods;  
  3. // 文具清单  
  4. vector<string> vecStationaries;  
  5. vecStationaries.push_back("Pen");  
  6. vecStationaries.push_back("Notes");  
  7. // 办公用品清单  
  8. vector<string> vecOfficeSupplies;  
  9. vecOfficeSupplies.push_back("Pen");  
  10. vecOfficeSupplies.push_back("Files");  
  11.   
  12. //调整容器大小  
  13. vecGoods.resize(vecOfficeSupplies.size() + vecStationaries.size());  
  14. //对源容器进行排序  
  15. sort(vecStationaries.begin(), vecStationaries.end());  
  16. sort(vecOfficeSupplies.begin(), vecOfficeSupplies.end());  
  17.   
  18. // 使用set_union() 合并到目标容器  
  19. // set_union() 返回指向合并后的目标容器中最后一个数据的迭代器  
  20. vector<string>::iterator itEnd =   
  21.     set_union(vecStationaries.begin(), vecStationaries.end(),  
  22.                   vecOfficeSupplies.begin(), vecOfficeSupplies.end(),  
  23.                    vecGoods.begin());  
  24. //输出合并后的商品  
  25. for (vector<string>::iterator it  = vecGoods.begin(); it != itEnd; ++it)  
  26. {  
  27.     cout<<*it<<endl;  
  28. }  
另外:STL还提供了set_difference()用于计算两个容器的差集,set_intersection()用于计算两个容器的交集

五、变换容器元素 transform()

在复制元素时,有时候需要对元素进行某些操作。例如,希望将某个容器的数据 变为原来的两倍。

copy()能够移动数据,但是无法在移动过程中对数据进行操作。transform()就是干这个事情的

原型:

[cpp]  view plain copy
  1. template<class InputIterator, class OutputIterator, class UnaryFunction>  
  2.    OutputIterator transform(  
  3.       InputIterator _First1,   
  4.       InputIterator _Last1,   
  5.       OutputIterator _Result,  
  6.       UnaryFunction _Func //算法的操作函数  
  7.    );  
  8. template<class InputIterator1, class InputIterator2, class OutputIterator,  
  9.    class BinaryFunction>  
  10.    OutputIterator transform(  
  11.       InputIterator1 _First1,   
  12.       InputIterator1 _Last1,  
  13.       InputIterator2 _First2, //第二个参数起始位置  
  14.       OutputIterator _Result,  
  15.       BinaryFunction _Func  
  16.    );  

例子:大学考试后,老师调整55分以上,60分以下的为60分

[cpp]  view plain copy
  1. / 定义移动数据过程中的操作函数  
  2. int Increase(int nScore)  
  3. {  
  4.     if (nScore > 55 && nScore < 60)  
  5.     {  
  6.         nScore = 60;  
  7.     }  
  8.     return nScore;  
  9. }  
  10.   
  11. vector<int> vecScore;  
  12.   
  13. vecScore.push_back(26);  
  14. vecScore.push_back(56);  
  15. vecScore.push_back(72);  
  16. //对容器中数据进行处理  
  17. transform(vecScore.begin(), vecScore.end(),  //输出数据的范围  
  18.           vecScore.begin() ,                      ///保存处理结果的容器的起始位置  
  19.           Increase);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值