The C++ Boost Libraries (Part 2 – boost::assign)
January 18, 2009 – 11:17 pmWe are still only in the low lands of boost territory but already we are coming across useful discoveries. Today’s stop is boost::assign, one of those clever little pieces of code that makes life easier for everyone. Often you just want to load up a container with some small amount of data. The STL containers do not make such a task particularly easy:
faceCards.push_back(“jack”);
faceCards.push_back(“queen”)
faceCards.push_back(“king”);
faceCards.push_back(“ace”);
What a pain in the neck! With boost::assign it becomes:
faceCards += “jack”,”queen”, “king”, “ace”;
Neat.
It gets better. A lot of the time you want to fill a container with some data for testing but you don’t really care what that data is:
// data will contain 1,2,5,5,5,5,5,5,5,5,7
data += 1, 2, repeat(8, 5), 7;
Or even:
// data will contain 1,2,(8 random numbers),7
randomData += 1, 2, repeat_fun(8, &rand),7;
Of course, other containers are supported. This example uses the more flexible insert function (there are other functions for inserting at specific points for containers that support such things):
insert(maoriColors)(“ma”, “white”)
(“whero”, “red”)
(“kakariki”, “green”)
(“kowhai”, “yellow” );
As with all the boost libraries, a lot of thought has gone into to making the interface safe and flexible. The library can even be extended to non-standard containers if the need should arise.
All the best magic tricks are really just smoke and mirrors, boost::assign is really just functions that secretly return functor objects and operator,() abuse. Unlike most magic, knowing how it works makes it even more delightful; the library is very well documented.
Although boost::assign is useful anywhere, it really shines in simple throwaway programs and test harnesses, where small, simple and easily modifiable code is the goal. I keep finding more and more places to use it.
Related posts:
- The C++ Boost Libraries Part 6 – boost::any In C++ if you have a variable that you say...
- Using Exceptions in C++ C++ is big – it has been said that any...
Related posts brought to you by Yet Another Related Posts Plugin.
5 Responses to “The C++ Boost Libraries (Part 2 – boost::assign)”
why are your template arguments missing? secondly, wouldn’t it be better if you give the corresponding libraries boost links?
By comptrol on May 3, 2009
comptrol: I just made a similar comment on another post. It just occured to me, I think WordPress is dropping/not encoding the angle brackets correctly and then FireFox is interpreting <string> as a html tag.
Andrew: There are a few syntax highlighting plugins which may help with this.
By Chris on May 30, 2009
You are both right, I usually go through and fix up the markup for my C++ code but I screwed up this post.
By Andrew on May 31, 2009
For the newbie:
Please don’t forget this:
using namespace boost::assign
other wise ‘operator+=’, ‘operator,’ etc will not be resolved.
By Tarun Elankath on Jun 2, 2009
Tarun, you are correct. I probably should have mentioned that in the post.
Both the following lines are assumed in the code samples:
using namespace std;
using namespace boost::assign;
By Andrew on Jun 2, 2009