Number of elements greater than 9: count_if and greater() : random_shuffle « STL Algorithms Modifying sequence operations « C++ Tutorial






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

using namespace std;

bool greater9( int );

int main()
{
   const int SIZE = 10;
   int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   vector< int > v( a1, a1 + SIZE );

   random_shuffle( v.begin(), v.end() );

   int a2[] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
   vector< int > v2( a2, a2 + SIZE );
 
   int result = count_if( v2.begin(), v2.end(), greater9 );
   cout << "\nNumber of elements greater than 9: " << result;

   return 0;
}

bool greater9( int value ) { return value > 9; }








24.7.random_shuffle
24.7.1.std::random_shuffle a vector
24.7.2.Use back_insert_iterator to insert element into a vector
24.7.3.Use random_shuffle to shuffle all elements randomly
24.7.4.Use random_shuffle to shuffle elements with self-written random number generator
24.7.5.Shuffle a sequence with random_shuffle()
24.7.6.shuffle the container with random_shuffle
24.7.7.Number of elements greater than 9: count_if and greater()