tags:

views:

3657

answers:

21

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?

+34  A: 

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

fhe
Also probably most over-used. I have myself learnt the lesson the hard way by having to refactor out most of the usages of shared_ptr by references, pointer containers and auto_ptr. I now mostly agree to this: http://www.bureau14.fr/blogea/index.php/2009/08/smart-pointers-are-overused/
Amit Kumar
+2  A: 

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 programers, that's why it's the test bed for future standard library.

Klaim
+1  A: 

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

Robert Gould
Smart pointers are RAII.
Eclipse
more precisely, Smart pointers give you RAII when there's no choice but to allocate memory dynamically.
Branan
+4  A: 

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

tgamblin
+3  A: 

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.

ckarmann
+1  A: 

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.

Martin Beckett
Check our oost::format.
Rob
e.g.:`std::string = boost::format("Hello, %1% %2%") % "world" % "!!!").str();`
Rob
If you are willing to forgo type-safety, you can roll your own with vsnprintf(), ellipsis (...), va_list/stdarg.h, and a local (stack-based) buffer.
Mr.Ree
std::string already has 71 functions too many (by Herb Sutter's count, not mine). See http://www.gotw.ca/gotw/084.htm for details: I think it has enough info to explain (a) why format needn't be in std::string, and (b) why it's better to write generic algorithms than class member functions anyway.
Steve Jessop
Or to put it another way, "C++ is like a foreign country: they do things differently there" ;-)
Steve Jessop
Boost to the rescue! I guess Format isn't in the std lib because streams are apparently the greatest thing since sliced bread and format looks like nasty old 'c'.
Martin Beckett
Format's not part of the library because one of the challenges Stroustrup was posed as he was designing C++ was the construction of a type-safe formatted I/O library. Obviously, the result was what you see with iostreams. Apparently, no one had thought of interpolation at the time. Perhaps someone would like to write a formatstream, to make traditionalists feel more at home?
Novelocrat
+17  A: 

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.

Rob
+11  A: 

Nobody mentions boost::tuple? For shame!

Arkadiy
+6  A: 

BOOST_STATIC_ASSERT

uvts_cvs
BOOST_MPL_ASSERT_MSG allows very easy to read/spot errors that are far more informative than the sizeof incomplete type message that BOOST_STATIC_ASSERT gives.
KitsuneYMG
here here! I just found one of these incomplete type errors inside the testing macro BOOST_CHECK_CLOSE - took me half a day to figure out what was going on before I twigged that I'd called it with (int,int,float); once I cast the integers to floating point the error went away. But what that has to do with an incomplete type I really don't know :)
Jamie Cook
+6  A: 

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.

D.Shawley
+9  A: 

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.

Head Geek
+6  A: 

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.

Kaz Dragon
I agree with all of the above -- except Lambda. I used it for a while, but it's so tortuous that I've abandoned it for all but the simplest expressions. Eagerly awaiting C++0x and its form of lambda expressions.
Head Geek
I agree that Boost.Lambda is full of all sorts of pitfalls -- as soon as I enter the realms of Unlambda or Protect, I give up and do it the old way, but it seems essential in extending callbacks in any half-decent way. That said, I too await the C++0x implementation.
Kaz Dragon
+3  A: 

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;
hamishmcn
+12  A: 

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.

Greg Rogers
+22  A: 

BOOST_FOREACH makes life worthwhile again.

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

Paul Biggar
Eric Niebler's article on "Conditional Love" (http://www.artima.com/cppsource/foreach.html) describes how BOOST_FOREACH works. It's pretty crazy.
Jeff Hardy
+12  A: 

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);
        fp = NULL;
    }
}
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" );
}
hamishmcn
+4  A: 

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

Jon Reid
I like it, I shall check out the ASL
hamishmcn
+2  A: 

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

Glorphindale
+3  A: 

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

Copperpot
+1  A: 

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

Vicente Botet Escriba
+2  A: 

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

Amit Kumar