TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm rotate_copy() function

C++ algorithm rotate_copy() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm rotate_copy()

C++ Algorithm rotate_copy() function is used to make a rotated copy of the elements in the range [first, last).

  • The sequence will start at the element in the middle of the source sequence and the last element will be followed by first.
  • It appends the element between the first and the middle to the elements between the middle and the last element.

Syntax

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

Parameter

first: A forward iterator pointing the position of the first element in the range to be rotated.

middle: A forward iterator addressing to the element within the range [first, last) that is moved to the first position in the range.

last: A forward iterator pointing the position one past the final element in the range in which the elements are being reversed.

result: An output iterator pointing the position of the first element in the destination range.

Return value

rotate_copy() function returns an output iterator addressing to the end of the copied range.

Complexity

Complexity is linear in the range [first, last): performs an assignment for each element.

Data races

The object in the range [first, last) are accessed.

The object in the range between result and the returned value are changed.

Exceptions

This function throws an exception if either an element assignment or an operation on iterator throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to rotate the given string:

#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;
}

Output:

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

Example 2

Let's see another simple example:

#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;
}

Output:

3 4 5 1 2

Example 3

Let's see another simple example:

#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;
}

Output:

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  

Example 4

Let's see another simple example:

#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;
}

Output:

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

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf