C++ STL交换替换

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
#include <set>
#include <cstdlib>
#include <functional>
using namespace std;

struct string_length_exceeds
{
 string_length_exceeds(int n): limit(n){}
 bool operator()(const string& s)
 {
  return s.size()>limit;
 }
 int limit;
};

int main()
{
 char A[]="abcdefgh";
 vector<char> V(A,A+strlen(A));

 list<char> L;
 copy(V.begin(),V.end(),back_inserter(L));
 copy(L.begin(),L.end(),ostream_iterator<char>(cout," "));

 swap()//
 int x=1;
 int y=2;
 swap(x,y);
 cout<<x<<" "<<y<<endl;

 ///swap_ranges///

 vector<int> v1;
 v1.push_back(1);
 v1.push_back(2);

 vector<int> v2;
 v2.push_back(3);
 v2.push_back(4);

 swap_ranges(v1.begin(),v1.end(),v2.begin());
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 
 copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
 cout<<endl;

 transform//
 const int N=7;
 double B[N]={1,2,3,4,5,6,7};
 list<double> L3(N);

 transform(B,B+N,L3.begin(),negate<double>());
 copy(L3.begin(),L3.end(),ostream_iterator<double>(cout," "));
 cout<<endl;

 ///replace///
 vector<string> fruits;
 fruits.push_back("cherry");
 fruits.push_back("apple");
 fruits.push_back("peach");
 fruits.push_back("apple");
 fruits.push_back("cherry");
 replace(fruits.begin(),fruits.end(),string("apple"),string("orange"));
 copy(fruits.begin(),fruits.end(),ostream_iterator<string>(cout," "));
 cout<<endl;

 /replace_if()/
 vector<double> Ve;
 Ve.push_back(1);
 Ve.push_back(-3);
 Ve.push_back(2);
 Ve.push_back(-1);
 replace_if(Ve.begin(),Ve.end(),bind2nd(less<double>(),0),11);
 copy(Ve.begin(),Ve.end(),ostream_iterator<double>(cout," "));
 cout<<endl;
 
 ///replace_if()///
 string str[7]={"oxygen","carbon","nitrgen","iron","sodiym","hydrogen","silicon"};
 replace_if(str,str+7,string_length_exceeds(6),"&&&");
 copy(str,str+7,ostream_iterator<string>(cout," "));
 cout<<endl;

 //replace_copy()
 

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值