STL algorithm算法swap,swap_ranges(56)

swap原型:

std::swap

header
// moved from <algorithm> to <utility> in C++11
non-array (1)
template <class T> void swap (T& a, T& b)
  noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value);
array (2)
template <class T, size_t N> void swap(T (&a)[N], T (&b)[N])
  noexcept (noexcept(swap(*a,*b)));
交换两个对象(也可以是两个容器)的值。

行为类似于:

template <class T> void swap (T& a, T& b)
{
  T c(std::move(a)); a=std::move(b); b=std::move(c);
}
template <class T, size_t N> void swap (T (&a)[N], T (&b)[N])
{
  for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);
}
一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void mswap()
{
    vector<int> vi{1,8,5,19,22,5,4};
    vector<int> v2{1,45,88,33,15,0,-8};
    cout<<"at first:"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"v2=";
    for(int i:v2)
        cout<<i<<" ";
    cout<<endl;
    swap(vi,v2);
    cout<<endl<<"after swap(vi,v2)"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"v2=";
    for(int i:v2)
        cout<<i<<" ";
    cout<<endl;
}
运行结果:






swap_ranges原型:

std::swap_ranges

template <class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
                                ForwardIterator2 first2);
交换两个范围的元素。

第二个范围从first2开始。

其行为类似于:

template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
                                ForwardIterator2 first2)
{
  while (first1!=last1) {
    swap (*first1, *first2);
    ++first1; ++first2;
  }
  return first2;
}
一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void mswapranges()
{
    vector<int> vi{1,8,5,19,22,5,4};
    vector<int> v2{1,45,88,33,15,0,-8};
    cout<<"at first:"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"v2=";
    for(int i:v2)
        cout<<i<<" ";
    cout<<endl;
    swap_ranges(vi.begin(),vi.end(),v2.begin());
    cout<<endl<<"after swap_range(vi.begin(),vi.end(),v2.begin())"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"v2=";
    for(int i:v2)
        cout<<i<<" ";
    cout<<endl;
}


运行截图:





——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-26

于GDUT

——————————————————————————————————————————————————





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值