简体   繁体   中英

Reverse vector, and build another vector

I was wondering if there was a way to reverse a vector and build that into another vector?

#include "std_lib_facilities.h"

int main()
{
    vector<int> oldvector;
    oldvector[1];
    oldvector[3];
    oldvector[5];
    oldvector[7];
    oldvector[9];
}

this is what i have so far, any help would be great.

If you want to create a new vector then you can do this:

std::vector<int> oldvector = {1,2,3,4,5};
std::vector<int> newvector(oldvector.rbegin(), oldvector.rend());

For reverisng a vector you can simply use

std::reverse(a.begin(), a.end());

and then use it as a new vector.

You may want to use std::reverse() to reverse the vector in place , or reverse iterators returned by std::vector::rbegin() / rend() to build a reversed copy of the original vector.

Consider the following compilable sample code ( live here on Ideone.com ):

#include <algorithm>    // For std::reverse
#include <iostream>     // For console output
#include <vector>       // For std::vector
using namespace std;

// Used to print elements of a sequence (like vector).
template <typename Sequence>
void print(const Sequence& s) {
    cout << "[ ";
    for (const auto& x : s) {
        cout << x << ' ';
    }
    cout << "]" << endl;
}

int main() {
    cout << "Original vector:\n";
    vector<int> v{10, 20, 30, 40, 50};
    print(v);

    vector<int> v2(v.rbegin(), v.rend());
    cout << "\nReversed copy using reverse iterators:\n";
    print(v2);

    reverse(v.begin(), v.end());    
    cout << "\nReversed in-place using std::reverse():\n";
    print(v);
}

Output:

 Original vector: [ 10 20 30 40 50 ] Reversed copy using reverse iterators: [ 50 40 30 20 10 ] Reversed in-place using std::reverse(): [ 50 40 30 20 10 ] 

There are several approaches to do the task. The simplest way is to use either the constructor or member function assign with parameters as iterators

For example

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::vector<int> v2( v1.rbegin(), v1.rend() );

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;
}

or

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::vector<int> v2;

    v2.assign( v1.rbegin(), v1.rend() );

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;
}

Another approach is to use standard algorithm std::reverse_copy . For example

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

int main()
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::vector<int> v2;
    v2.reserve( v1.size() ); 

    std::reverse_copy( v1.begin(), v1.end(), std::back_inserter( v2 ) );

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;
}

Or you can write the same using an ordinary loop

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::vector<int> v2;
    v2.reserve( v1.size() ); 

    for ( std::vector<int>::size_type i = v1.size(); i != 0; )
    {
        --i;
        v2.push_back( v1[i] );
    }

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;
}

Or even you can use a stack as an intermediate container!:)

#include <iostream>
#include <vector>
#include <stack>

int main() 
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::stack<int> s;

    for ( int x : v1 ) s.push( x );

    std::vector<int> v2;
    v2.reserve( v1.size() );

    while ( !s.empty() ) 
    {
        v2.push_back( s.top() );
        s.pop();
    }

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;



    return 0;
}

And what about a recursive function? Well, it could look for example as

#include <iostream>
#include <vector>

std::vector<int>::iterator reverse( std::vector<int>::const_iterator first,
                                    std::vector<int>::const_iterator last,
                                    std::vector<int>::iterator out  )
{
    if ( first != last )
    {
        int x = *first++;
        out = ::reverse( first, last, out );
        *out++ = x;
    }

    return out;
}

int main() 
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::vector<int> v2( v1.size() );

    ::reverse( v1.cbegin(), v1.cend(), v2.begin() );

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;


    return 0;
}

I am sorry! I forgot to mention standard algorithm std::copy_backward . It also can be applied

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

int main() 
{
    std::vector<int> v1 = { 1, 3, 5, 7, 9 };
    std::vector<int> v2( v1.size() );

    std::copy_backward( v1.begin(), v1.end(), v2.rend() );

    for ( int x : v1 ) std::cout << x << ' ';
    std::cout << std::endl;

    for ( int x : v2 ) std::cout << x << ' ';
    std::cout << std::endl;


    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM