TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm reverse_copy() function

C++ algorithm reverse_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 reverse_copy()

C++ Algorithm reverse_copy() function is used to copy the elements from the range[first, last) to another range beginning at result in such a way that the elements in the range are in reverse order.

Syntax

template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first,
BidirectionalIterator last, OutputIterator result);

Note: BidirectionalIterator is an iterator which is used to access any elements of a container in both forward and backward direction.

Parameter

first: A bidirectional iterator pointing the position of the first element in the range in which the elements are being reversed.

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

result: Output iterator pointing to the initial position of the range to which elements are being copied.

Return value

This function returns an output iterator pointing to the end of the copied range [first, last) to where the altered sequence of elements is being copied.

Complexity

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

Data races

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

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

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 demonstrate the use of reverse_copy():

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

using namespace std;
 
int main()
{    
    vector<int> v({1,2,3});
    cout << "Before: ";
    for (const auto& value : v) {
        cout << value << " ";
    }
    cout << '\n';

    vector<int> destination(3);
    reverse_copy(begin(v), end(v), begin(destination));
    cout << "After:  ";
    for (const auto& value : destination) {
        cout <<value << " ";
    }
    cout << '\n';
    
    return 0;
}

Output:

Before: 1 2 3 
After:  3 2 1

Example 2

Let's see another simple example to reverse the string:

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

using namespace std;

int main() {
  string str = "Hello Myself Nikita";
  cout<<"Before Reverse: "<< str<< endl;
  
  cout<<"After Reverse: ";
  reverse_copy(str.begin(), str.end(),
    ostream_iterator<char>(cout, ""));
    
    return 0;
}

Output:

Before Reverse: Hello Myself Nikita
After Reverse: atikiN flesyM olleH

Example 3

Let's see another simple example to reverse the range of numbers:

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main( ) {  
   using namespace std;  
   vector <int> v1, v2( 10 );  
   vector <int>::iterator Iter1, Iter2;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
   {  
      v1.push_back( i );  
   }  
  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Reverse the elements in the vector   
   reverse_copy (v1.begin( ), v1.end( ), v2.begin( ) );  
  
   cout << "The copy v2 of the reversed vector v1 is:\n ( " ;  
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )  
      cout << *Iter2 << " ";  
   cout << ")." << endl;  
  
   cout << "The original vector v1 remains unmodified as:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}  

Output:

The original vector v1 is:
 ( 0 1 2 3 4 5 6 7 8 9 ).
The copy v2 of the reversed vector v1 is:
 ( 9 8 7 6 5 4 3 2 1 0 ).
The original vector v1 remains unmodified as:
 ( 0 1 2 3 4 5 6 7 8 9 ).

Example 4

Let's see another simple example:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <iterator>

using namespace std;
 
void print(string a[], int N)
{   
    for(int i = 0; i < N; i++)
    {
        cout << (i + 1) << ". " << setw(5)
             << a[i] << "  ";
    }
    cout << endl;
}
 
int main()
{
    string s[] = {"George", "John", "Nikki", "Alice", "Bob", "Watson"};
    string t[6];
 
    cout << "Original order : ";
    print(s, 6);
    cout << "\nReversing the order ... " << endl;
    // Doesn't modify original array s[]
    reverse_copy(s, s + 6, t);
    cout << "Reversed order : ";
    print(t, 6);
    
    return 0;
}

Output:

Original order : 1. George  2.  John  3. Nikki  4. Alice  5.   Bob  6. Watson  

Reversing the order ... 
Reversed order : 1. Watson  2.   Bob  3. Alice  4. Nikki  5.  John  6. George  

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