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 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <algorithm>
#include <list>
int main ()
{
const int numlist[] {15,18,2,19,18,0,8,14,19,14};
std::cout << "Pos\tValue\n";
for ( int i = 0; int num : numlist )
{
std::cout << i++ << '\t' << num << '\n';
}
int value {};
std::cout <<"Value to seek : \n";
std::cin >> value;
size_t count = 0;
auto it = std::find_if( std::begin(numlist), std::end(numlist),
[&count, value](int x){ ++count; return x == value; } );
if ( it != std::end(numlist) ) {
auto index = std::distance( std::begin(numlist), it );
std::cout << "The value " << value << " is at position " << index << '\n';
}
else {
std::cout << "The value " << value << " not found\n";
}
std::cout << count << " comparisons\n\n";
std::list<int> numar( std::begin(numlist), std::end(numlist) );
for ( int i = 0; int num : numar )
{
std::cout << i++ << '\t' << num << '\n';
}
count = 0;
auto it2 = std::find_if( begin(numar), end(numar),
[&count, value](int x){ ++count; return x == value; } );
if ( it2 != end(numar) ) {
auto index = std::distance( begin(numar), it2 );
std::cout << "The value " << value << " is at position " << index << '\n';
}
else {
std::cout << "The value " << value << " not found\n";
}
std::cout << count << " comparisons\n\n";
auto occ = std::count( std::begin(numlist), std::end(numlist), value );
std::cout << occ << " occurrences of " << value << '\n';
}
|