The C++ Boost Libraries (Part 2 – boost::assign)

January 18, 2009 – 11:17 pm

We 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:

vector<string> faceCards;
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:

vector<string> faceCards;
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:

list<int> data;
// data will contain 1,2,5,5,5,5,5,5,5,5,7
data += 1, 2, repeat(8, 5), 7;

Or even:

list<int> randomData;
// 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):

map<string, string> maoriColors;
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.

Post to Twitter Post to Facebook Post to Reddit Post to StumbleUpon

Related posts:

  1. The C++ Boost Libraries Part 6 – boost::any In C++ if you have a variable that you say...
  2. Using Exceptions in C++ C++ is big – it has been said that any...

Related posts brought to you by Yet Another Related Posts Plugin.

  1. 5 Responses to “The C++ Boost Libraries (Part 2 – boost::assign)”

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word