Monday, February 25, 2008

std::fill_n

Many of the tests I have been writing lately involve creating min/max/misc. values to send across our messaging system. It dawned on me today that the for loops I have been writing could be better expressed with std::fill_n. For example take the following loop:
std::vector<std::string> strSeq;
const int MAX_STRSEQ_SIZE = 31;
for (int i = 0; i < MAX_STRSEQ_SIZE; ++i)
strSeq.push_back("0123456789");
This can be better written using the standard template library:
std::vector<std::string> strSeq;
const int MAX_STRSEQ_SIZE = 31;
std::fill_n(std::back_inserter(strSeq), MAX_STRSEQ_SIZE,
"0123456789");
Although this isn't really any shorter than the original for loop, I believe it reads better.

Lets review std::fill_n():
template<typename ForwardIter, typename Size, typename T>
fill_n(ForwardIter begin, Size n, const T& value);
std::fill_n takes three arguments: ForwardIter, Size, and T. ForwardIter is designed to take an iterator pointing to where to begin filling. However, since std::fill_n assumes there is room in the container we use std::back_inserter() to adapt the assignment operator into a push_back on our container. Size is how many of T to assign and T is the value to assign.

No comments: