Thursday, March 15, 2012

boost::assign my new friend

On my current project we do a lot of unit testing sometimes with ‘random’ data. No biggie right… well when you consider the fact that a lot of the attributes being tested are very large arrays and vectors…. coming up with random values can be time consuming and tedious… but luckily I found:
      boost::assign

Being able to set a vector quickly with the ‘+=’ operator is amazing, plus toss in the ‘repeat’ and ‘repeat_fun’ command and I’m golden.
#include <boost/assign/std/vector.hpp>
#include <cstdlib> // for ‘rand()’
// bring ‘operator+=()’ into scope
using namespace boost::assign;
const int LARGE_ARRAY_SIZE = 4200;

std::vector<double> largeV;
largeV += 0.1, repeat_fun(LARGE_ARRAY_SIZE - 1, &rand);
simple as that… not I’ve got a vector containing 4200 items. plus using boost::multi_array and some reshaping and I can massage that into a 2x2100 matrix or any size.

No comments: