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 36 37 38
|
#include <vector>
#include <functional>
#include <stdexcept>
#include <iostream>
#include <iomanip>
std::vector<std::size_t> index_if( const std::vector<int> & u, const std::vector<int>& v,
std::function<bool(int,int)> select )
{
if ( u.size() != v.size() )
throw std::logic_error( "vector size mismatch in index_if" ) ;
std::vector<std::size_t> indices ;
for ( std::size_t i=0; i<u.size(); ++i )
if ( select(u[i], v[i]) )
indices.push_back(i) ;
return indices ;
}
int main()
{
int uData[] = { 3, 8, 20, 50, 10, 2, 85, 23, 40, 3 } ;
int vData[] = { 6, 40, 33, 100, 3, 40, 5, 10, 36, 10 } ;
const std::vector<int> u(uData, uData + (sizeof(uData)/sizeof(uData[0]))) ;
const std::vector<int> v(vData, vData + (sizeof(vData)/sizeof(vData[0]))) ;
const int x1 = 15, x2 = 60 ;
const int x3 = 10, x4 = 50 ;
auto indices = index_if(u, v, [=](int a, int b)->bool
{return a >= x1 && a <= x2 && b >= x3 && b <= x4;}) ;
std::cout << std::setw(5) << "U" << std::setw(5) << "V\n" ;
for ( auto i : indices )
std::cout << std::setw(5) << u[i] << std::setw(5) << v[i] << '\n' ;
}
|