1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <vector>
#include <functional>
#include <iomanip>
#include <valarray>
int main()
{
{
std::vector<int> seq { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65 } ;
for( int v : seq ) std::cout << v << ' ' ;
std::cout << " (values)\n" ;
for( std::size_t pos = 0 ; pos < seq.size() ; ++pos )
std::cout << std::setw(2) << pos << ' ' ;
std::cout << " (original order)\n" ; ;
std::vector<std::size_t> order { 1, 9, 5, 7, 11, 2, 3, 8, 4, 6, 0, 10 } ;
for( std::size_t pos : order ) std::cout << std::setw(2) << pos << ' ' ;
std::cout << " (required order)\n" ; ;
std::vector< std::reference_wrapper<int> > indirect ;
for(std::size_t pos : order ) indirect.emplace_back( seq[pos] ) ;
for( int v : indirect ) std::cout << v << ' ' ;
std::cout << " (indirect)\n" ; ;
}
{
std::valarray<int> seq { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65 } ;
std::valarray<std::size_t> order { 1, 9, 5, 7, 11, 2, 3, 8, 4, 6, 0, 10 } ;
std::valarray<int> indirect { seq[order] } ;
for( int v : indirect ) std::cout << v << ' ' ;
std::cout << " (indirect)\n" ; ;
}
}
|