C++类属性swap_ranges

类属性算法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 }

原文地址:https://www.cnblogs.com/djcsch2001/p/2060253.html