Removing All Matching Elements
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
using namespace std;
int main( )
{
const int last_number = 50;
vector<int> v( last_number - 1, 1 );
v[0] = 2;
partial_sum( v.begin(), v.end(), v.begin() );
vector<int>::iterator stop = v.end();
for( vector<int>::iterator start = v.begin(); start != stop;++start )
// remove all subsequent numbers that are not divisible by the current one
stop = remove_if( start+1, stop,not1( bind2nd( modulus<int>(), *start ) ) );
cout << "Prime numbers: ";
copy( v.begin(), stop, ostream_iterator<int>( cout, " " ) );
}
Related examples in the same category