C++ 算法 rotate_copy()函数

C++ 算法 rotate_copy()函数

C++算法 rotate_copy() 函数用于创建一个旋转复制的序列,范围是[first, last)。

  • 序列将以源序列中间的元素开始,最后一个元素将跟在第一个元素后面。
  • 它将第一个和中间之间的元素追加到中间和最后一个元素之间的元素中。

语法

template <class ForwardIterator, class OutputIterator>
OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle,
ForwardIterator last, OutputIterator result);

参数

first :一个前向迭代器,指向要旋转的范围中的第一个元素的位置。

middle :一个前向迭代器,指向范围[first, last)中要移动到范围中的第一个位置的元素。

last :一个前向迭代器,指向范围中最后一个元素的下一个位置,该范围内的元素将被反转。

result :一个输出迭代器,指向目标范围中第一个元素的位置。

返回值

rotate_copy()函数返回一个输出迭代器,指向复制范围的末尾。

复杂度

复杂度在范围[first, last]中是线性的 :对每个元素执行一次赋值操作。

数据竞争

在范围[first, last)中的对象被访问。

在结果和返回值之间的范围中的对象被改变。

异常

如果元素赋值或迭代器操作引发异常,则此函数会抛出异常。

注意:无效的参数会导致未定义的行为。

示例1

让我们来看一个简单的示例,将给定的字符串旋转:

#include <algorithm>
#include <iostream>
#include <string>
#include <iterator>

using namespace std;

int main() {
  string str = " N I K I T A";
  string result;
 cout << "Before Rotate : "<< str << endl;
  rotate_copy(str.begin(), str.begin() + 2, str.end(),
    back_inserter(result));

  cout <<"After Rotate  : " << result << endl;

  return 0;
}

输出:

Before Rotate :  N I K I T A
After Rotate   :  I K I T A N

示例2

让我们来看另一个简单的示例:

#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> src = {1, 2, 3, 4, 5}; 
    auto pivot = find(src.begin(), src.end(), 3); 
    vector<int> dest(src.size());                                          

    rotate_copy(src.begin(), pivot, src.end(), dest.begin());

    for (const auto &i : dest) {
        cout << i << ' ';
    }   
    cout << '\n';

    return 0;
}

输出:

3 4 5 1 2

示例3

让我们来看另一个简单示例:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;

void print(char a[], int N)
{   
    for(int i = 0; i < N; i++)
    {
        cout << (i + 1) << ". " << setw(1)
             << left << a[i] << "  ";
    }
    cout << endl;
}

int main()
{
    char s[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
    int slen = sizeof(s) / sizeof(char), tlen = slen;
    char t[tlen];

    cout << "Character array s[] : ";
    print(s, slen);
    cout << "Rotate s[] with \'C\' as middle element and copy in t[]" << endl;
    rotate_copy(s, s + 2, s + slen, t);
    cout << "Character array t[] : ";
    print(t, tlen);
    cout << "Rotate t[] with \'A\' as middle element and copy in s[]" << endl;
    rotate_copy(t, t + 6, t + tlen, s);
    cout << "Character array s[] : ";
    print(s, slen);
    cout << "Character array t[] : ";
    print(t, tlen);

    return 0;
}

输出:

Character array s[] : 1. A  2. B  3. C  4. D  5. E  6. F  7. G  8. H  
Rotate s[] with 'C' as middle element and copy in t[]
Character array t[] : 1. C  2. D  3. E  4. F  5. G  6. H  7. A  8. B  
Rotate t[] with 'A' as middle element and copy in s[]
Character array s[] : 1. A  2. B  3. C  4. D  5. E  6. F  7. G  8. H  
Character array t[] : 1. C  2. D  3. E  4. F  5. G  6. H  7. A  8. B  

示例4

让我们看另一个简单的示例:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std ;

int main()
{
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of strings
    typedef vector<string> StrVector ;

    //Define an iterator for template class vector of strings
    typedef StrVector::iterator StrVectorIt ;

    StrVector Tongue_Twister(VECTOR_SIZE) ;
    StrVector Rotated_Twister(VECTOR_SIZE) ;

    StrVectorIt start, middle, end, it, RTstart, RTend ;

    start = Tongue_Twister.begin() ;    // location of first
                                        // element of Tongue_Twister

    end = Tongue_Twister.end() ;        // one past the location last
                                        // element of Tongue_Twister

    middle = start + 3 ;                // start position for
                                        // rotating elements

    RTstart = Rotated_Twister.begin() ; // location of first
                                        // element of Rotated_Twister

    RTend = Rotated_Twister.end() ;     // one past the location last
                                        // element of Rotated_Twister

    //Initialize vector Tongue_Twister
    Tongue_Twister[0] = "she" ;
    Tongue_Twister[1] = "sells" ;
    Tongue_Twister[2] = "sea" ;
    Tongue_Twister[3] = "shells" ;
    Tongue_Twister[4] = "by";
    Tongue_Twister[5] = "the";
    Tongue_Twister[6] = "sea" ;
    Tongue_Twister[7] = "shore" ;

    cout << "Before calling rotate_copy:\n" << endl ;

    // print content of Tongue_Twister
    cout << "Try this Tongue Twister: " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << "\n\n" ;

    // rotate the items in the vector Tongue_Twist to the right by
    // 3 positions and copy the results to Rotated_Twister
    rotate_copy(start, middle, end, RTstart) ;

    cout << "After calling rotate_copy:\n" << endl ;

    // print content of Tongue_Twister
    cout << "Tongue_Twister: " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << "\n\n" ;

    // print content of Rotated_Twister
    cout << "Now try the rotated Tongue Twister: " ;
    for(it = RTstart; it != RTend; it++)
        cout << *it << " " ;
    cout << "\n\n" ;

    return 0;
}

输出结果:

Before calling rotate_copy:

Try this Tongue Twister: she sells sea shells by the sea shore 

After calling rotate_copy:

Tongue_Twister: she sells sea shells by the sea shore 

Now try the rotated Tongue Twister: shells by the sea shore she sells sea

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程