孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

类属性算法swap_ranges的作用是交换连个区间中的值,而且着两个区间可以在不同的容器中,例如

swap_ranges(first1,last1,first2)

上面的语句将区间[first1,last)和区间[first2,first+N)中的类荣相互交换,其中N=last1-first1.规定这两个区间不可以重叠

1 #include <iostream>
2 #include <cassert>
3 #include <algorithm>
4 #include <vector>
5 #include <string>
6 #include <cstring>
7 using namespace std;
8
9 template <typename Container>
10 Container make(const char s[])
11 {
12 return Container(&s[0],&s[strlen(s)]);
13 }
14
15 int main()
16 {
17 cout<<"Illustrating the generic swap_range lgorithm."<<endl;
18 vector<char> vector1=make< vector<char> >("HELLO"),
19 vector2=make< vector<char> >("THERE");
20
21 vector<char> temp1=vector1,temp2=vector2;
22 swap_ranges(vector1.begin(),vector1.end(),vector2.begin());
23
24 assert((vector1==temp2) && (vector2==temp1));
25 cout<<" --- OK."<<endl;
26 return 0;
27 }

posted on 2011-05-27 20:43  孤独的猫  阅读(1060)  评论(0编辑  收藏  举报