Thomas Sampson


Leave a comment

New STL RNG / Mersenne Engine

#include <random>
#include <iostream>
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1, 6);
    for(int n=0; n<10; ++n)
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}
Advertisements


Leave a comment

STL algorithm predicates

Just a quick reminder on how to set up a predicate function for an stl algorithm (accumulate shown here) using the () operator on a struct/class.

This arbitrary example accumulates all integer values in bag which are greater than 2. There third parameter to accumulate (the zero) means start accumulating from 0 rather than a given constant.

struct check
{
	int operator()(int l, int r)
	{
		return (r>2)?  (l+r): l;
	}
};

int total=std::accumulate(bag.begin(),bag.end(),0,check());