1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// int (&arr)[ARRAY_SIZE] -> pass array by reference
// so it doesn't decay into a pointer -> can't use sort/find/find_end
// if it is a pointer
// not sure on the above, maybe someone can correct me
void ranged_print( int (&arr)[ARRAY_SIZE], int low, int high )
{
sort( begin( arr ), end( arr ) );
auto start = find( begin( arr ), end( arr ), low ),
fin_ = find( begin( arr ), end( arr ), high ),
// find the last occurrence of high, in case of duplicates
fin = find_end( begin( arr ), end( arr ), fin_, fin_ + 1 ) + 1;
// prints [start, fin), hence the + 1 on fin
while( start != fin ) {
cout << *start << ' ';
start++;
}
}
int main( )
{
int arr[ARRAY_SIZE]{ 1, 4, 3, 5, 9, 7, 6, 10, 8, 4 };
ranged_print( arr, 3, 5 );
}
|