类属性算法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/archive/2011/05/27/2060253.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/26259
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!