This example is an "advanced" version of trsl_example1.cpp. Instead of creating the sample iterators within the code body, we hide the creation detail within a population class.
#include <iostream>
#include <cassert>
#include <functional>
#include <examples/Particle.hpp>
#include <examples/ParticleCollection.hpp>
int main()
{
using namespace trsl::example;
const size_t POPULATION_SIZE = 100;
const size_t SAMPLE_SIZE = 10;
ParticleCollection population;
for (size_t i = 0; i < POPULATION_SIZE; ++i)
{
Particle p(double(rand())/RAND_MAX,
double(rand())/RAND_MAX,
double(rand())/RAND_MAX);
population.add(p);
}
ParticleCollection sample;
for (ParticleCollection::const_sample_iterator
si = population.sample_begin(SAMPLE_SIZE),
sb = si,
se = population.sample_end();
si != se; ++si)
{
std::cout << "sample_" << std::distance(sb, si) << "'s weight = " <<
si->getWeight() << std::endl;
Particle p = *si;
p.setWeight(1);
sample.add(p);
}
assert(sample.size() == SAMPLE_SIZE);
return 0;
}