码迷,mamicode.com
首页 > 其他好文 > 详细

STL algorithm算法rotate,rotate_copy(51)

时间:2014-09-26 11:17:38      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:algorithm   算法   stl   c++11   

rotate原型:

std::rotate

template <class ForwardIterator>
  ForwardIterator rotate (ForwardIterator first, ForwardIterator middle,
                          ForwardIterator last);
该函数是循环移位函数。

效果是交换[middle,last)和[first,middle)部分的位置。

具体实现请自行搜索。

类似实现为:

template <class ForwardIterator>
  void rotate (ForwardIterator first, ForwardIterator middle,
               ForwardIterator last)
{
  ForwardIterator next = middle;
  while (first!=next)
  {
    swap (*first++,*next++);
    if (next==last) next=middle;
    else if (first==middle) middle=next;
  }
}
一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void mrotate()
{
    vector<int> vi{1,2,3,4,5,6};
    vector<int> vresult{10,20,30,40,50};
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"vresult=";
    for(int i:vresult)
        cout<<i<<" ";
    cout<<endl;
    rotate(vi.begin(),vi.begin()+3,vi.end());
    rotate(vresult.begin(),vresult.end()-1,vresult.end());
    cout<<"after     rotate(vi.begin(),vi.begin()+3,vi.end())"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"after rotate(vresult.begin(),vresult.end()-1,vresult.end());"<<endl;

    cout<<"vresult=";
    for(int i:vresult)
        cout<<i<<" ";
    cout<<endl;



}

运行截图:

bubuko.com,布布扣



rotate_copy原型:

std::rotate_copy

template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle,
                              ForwardIterator last, OutputIterator result);
该函数是将rotate后的序列存放到result开始的位置。

实现类似于:

template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle,
                              ForwardIterator last, OutputIterator result)
{
  result=std::copy (middle,last,result);
  return std::copy (first,middle,result);
}
一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void mrotatecopy()
{
    vector<int> vi{1,2,3,4,5,6};
    vector<int> vresult(6);
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"vresult=";
    for(int i:vresult)
        cout<<i<<" ";
    cout<<endl;
    rotate_copy(vi.begin(),vi.begin()+3,vi.end(),vresult.begin());
    cout<<"after rotate_copy(vi.begin(),vi.begin()+3,vi.end(),vresult.begin())"<<endl;
    cout<<"vi=";
    for(int i:vi)
        cout<<i<<" ";
    cout<<endl;

    cout<<"vresult=";
    for(int i:vresult)
        cout<<i<<" ";
    cout<<endl;



}


运行截图:

bubuko.com,布布扣


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

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

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

author:天下无双

Email:coderguang@gmail.com

2014-9-26

于GDUT

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




STL algorithm算法rotate,rotate_copy(51)

标签:algorithm   算法   stl   c++11   

原文地址:http://blog.csdn.net/qq844352155/article/details/39575513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!