Most used parts of Boost

C++Boost

C++ Problem Overview


When I discovered boost::lexical_cast I thought to myself "why didn't I know about this sooner!" - I hated having to write code like

stringstream ss;
ss << anIntVal;
mystring = ss.str();

Now I write

mystring = boost::lexical_cast<string>(anIntVal);

Yesterday, on stackoverflow, I came across boost split (another gem that will save me writing code).

string stringtobesplit = "AA/BB-CC")
vector<string> tokens;

boost::split(tokens, stringtobesplit, boost::is_any_of("/-")); 
// tokens now holds 3 items: AA BB CC

I am going to start looking through boost documentation looking for other functions that I will be able to use regularly, but I feel that it will be very easy to miss things.

What boost functions do you use most / would hate not to have?

C++ Solutions


Solution 1 - C++

Probably the most used part of boost for me is boost::shared_ptr.

Solution 2 - C++

BOOST_FOREACH makes life worthwhile again.

(Why has nobody mentioned this? The question was asked 8 months ago!)

Solution 3 - C++

My faves are, in no particular order:

  • regex
  • filesystem
  • thread
  • lexical_cast
  • program_options (just brilliant!)
  • test (for all my unit testing needs).
  • String algorithms
  • String tokenizer
  • format (type-safe printf style string formatting)
  • smart ptrs

Boost was a massive help when I wrote my first cross-platform app - without it I really would have struggled.

Solution 4 - C++

I like how you can supply your own destructor for shared_ptr.
This means, for example, you can use it with FILE* and get it to close the file for you.
eg

void safeclose(FILE*fp) {
    if(fp) {
        fclose(fp);
    }
}
void some_fn() {
    boost::shared_ptr<FILE> fp( fopen(myfilename, "a+t"), safeclose );
    //body of the function, and when ever it exits the file gets closed
    fprintf( fp.get(), "a message\n" );
}

Solution 5 - C++

Nobody has mentioned Multi-Index Containers so I'll chime in late. It's not that often that you need them, but without boost it is a real pain to create an equivalent data structure, as well as being less efficient. I've been using them a lot recently to create containers that have look up on 2 keys.

Solution 6 - C++

I'm surprised that no one has mentioned boost::optional. I find myself using it more often than any part of Boost except shared_ptr and scoped_ptr.

Solution 7 - C++

Nobody mentions boost::tuple? For shame!

Solution 8 - C++

BOOST_STATIC_ASSERT

Update (October 2011): C++11 (C++0x) has static_assert http://www2.research.att.com/~bs/C++0xFAQ.html#static_assert

Solution 9 - C++

One of my most used is not in Boost proper, but the Adobe Source Libraries (ASL) built on top of Boost — specifically, the extensions to the standard algorithms that accept a boost::range in place of separate begin/end iterators. Then instead of calling, say,

std::for_each(some_container.begin(), some_container.end(), do_something());

I can simply say

adobe::for_each(some_container, do_something());

(I do hope these parts of ASL migrate to Boost eventually.)

Solution 10 - C++

I use a lot:

  • boost::signals
  • boost::shared_ptr
  • boost::lexical_cast
  • boost::bind
  • boost::random
  • boost::thread
  • boost::noncopyable

Other like Tuple, Static Assert and Integer are very useful if you are writing a library which is due to be used on a variety of platforms.

Things like Graphs and Lambda are more specific.

Solution 11 - C++

boost::shared_ptr is a requirement for modern C++ programming IMHO. That's why they added it to the standard with TR1. boost::program_options, boost::bind, and boost::signal are really nice if you know what they are for and how to use them. The last two tend to scare newcomers though.

Solution 12 - C++

I've been using shared_ptr for years now. It's just so useful, there's no reason that a project should be without it.

On top of that, I also use Bind/Function/Lambda for generic callback mechanisms -- especially useful when testing -- as well as Format for my general-purpose sprintf replacement.

Finally, it was just the other day when I used Variant in anger to solve a problem (a parser that could respond with a small, fixed set of unrelated token types). The solution was very elegant, and I'm very happy with it.


Years have passed and times have changed, so time for an update. SharedPtr and Function are now part of the Standard, and Bind and Lambda are obsoleted by actual language-level lambda functionality.

I still use Variant (which has also been standardized, but I'm not there yet), Format is largely replaced by fmtlib (which has also been standardized).

The big part of Boost that I use is Boost.Asio. Which is in the process of being standardized.

Solution 13 - C++

We found boost::spirit pretty useful for a business solution to parse ECMAScript. Complex, but very nice!

Solution 14 - C++

I'm surprised to don't see yet between the answers Boost.Thread.

Solution 15 - C++

Using tuples to iterate a map, like this:

string key, value;
BOOST_FOREACH(tie(key, value), my_map) { ... }

Using boost assign, I can intialize a map like this:

map<string, string> my_map = map_list_of("key1", "value1")("key2", "value2")("key3", "value3");

And using range adaptors and the pipe("|") operator I can iterate backwards over the values of a map(as an example):

BOOST_FOREACH(string value, my_multimap.equal_range("X") | map_values | reversed) { ... }


Solution 16 - C++

You should check boost::program_options. It makes command line parsing much easier.

Solution 17 - C++

I use Boost Pointer Containers in preference to a STL container of shared_ptrs.

Solution 18 - C++

I use boost::numeric::ublas::matrix quite a bit.

Solution 19 - C++

What I use the most is now available in the TR1:

  • shared pointers
  • array class

Now I also use pool classes and some other more specific things.

You understand now that Boost is meant to be useful to most programmers, that's why it's the test bed for the future standard library.

Solution 20 - C++

I love boost::random and boost::asio and boost::filesystem, however boost::bind , boost::circular_buffer and boost::thread are very practical, smart pointers are ok but I prefer RAII instead as memory management

Solution 21 - C++

Okay, here is a new one I've found:
Instead of using stricmp I can use boost's equals function and pass in the is_iequal predicate
eg:
instead of

stricmp( "avalue", mystr.c_str() ) == 0

I can use

equals( "avalue", mystr, is_iequal() ) 

given:

#include <boost/algorithm/string.hpp>
using namespace boost::algorithm;

Solution 22 - C++

Here is my two cents:

  • boost::scope_exit - no need to define RAII class just for one use
  • boost::any
  • boost::variant
  • Boost Pointer Container Library (ptr_vector)
  • Boost Pool Library
  • boost::unordered_map / boost::unordered_set

Solution 23 - C++

I use boost::icl quite a lot for text post-processing. Saved me quite a lot of time because otherwise I'd have to implement text-splitting myself...

BOOST_FOREACH is everywhere in my code :)

boost::function and boost::bind are an absolute must. Although now they are std::function and std::bind. These really help reduce the amount of unnecessary code and are just generally good for my designs (or my delusions).

I've recently started using boost::interprocess::message_queue and this is a great tool too.

I'd use a lot more, but Qt has native ways of doing a lot of things Boost does. If I ever have to program pure C++, I guess I'd become a boost::junkie :)

Solution 24 - C++

Talking about boost::lexical_cast, why isn't something like 'format' a static member in the std::string library?
Almost all gui libs have something like CString::Format("%i") or QString::Number("%i") which return an initialised string.

Solution 25 - C++

I think the question should be reversed. Which part of you boost would you not want to use ?

In my experience pretty much all of it is interesting and useful in each problem domain.

You should spend time looking all around the boost documentation to find the areas that cover your interests.

One exception may be boost::numeric::ublas which is does its job, but Eigen does it remarkably better.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhamishmcnView Question on Stackoverflow
Solution 1 - C++fheView Answer on Stackoverflow
Solution 2 - C++Paul BiggarView Answer on Stackoverflow
Solution 3 - C++RobView Answer on Stackoverflow
Solution 4 - C++hamishmcnView Answer on Stackoverflow
Solution 5 - C++Greg RogersView Answer on Stackoverflow
Solution 6 - C++Head GeekView Answer on Stackoverflow
Solution 7 - C++user3458View Answer on Stackoverflow
Solution 8 - C++Alessandro JacopsonView Answer on Stackoverflow
Solution 9 - C++Jon ReidView Answer on Stackoverflow
Solution 10 - C++Cyrille KaView Answer on Stackoverflow
Solution 11 - C++D.ShawleyView Answer on Stackoverflow
Solution 12 - C++Kaz DragonView Answer on Stackoverflow
Solution 13 - C++Kit10View Answer on Stackoverflow
Solution 14 - C++Vicente Botet EscribaView Answer on Stackoverflow
Solution 15 - C++Paul Fultz IIView Answer on Stackoverflow
Solution 16 - C++Sad DeveloperView Answer on Stackoverflow
Solution 17 - C++amit kumarView Answer on Stackoverflow
Solution 18 - C++Todd GamblinView Answer on Stackoverflow
Solution 19 - C++KlaimView Answer on Stackoverflow
Solution 20 - C++Robert GouldView Answer on Stackoverflow
Solution 21 - C++hamishmcnView Answer on Stackoverflow
Solution 22 - C++anatolyView Answer on Stackoverflow
Solution 23 - C++ZeksView Answer on Stackoverflow
Solution 24 - C++Martin BeckettView Answer on Stackoverflow
Solution 25 - C++rodrigobView Answer on Stackoverflow