Boost C++ Libraries
What is Boost?
Many newcomers to C++ say things like "How come C++ can't do ...? Java's ... does ...." This comes from several philosphy differences.
- C++ is not an Object Oriented Only language, but multi-paradigmed (you will see many functions written that are part of no class).
- C++ Designers also believe classes should be "minimal, yet complete" (Some say std::string doesn't do enough, but the Standards Commitee regrets how bloated it is).
- The C++ Standards commitee also believe in not including policies and unproven designs in the language (SQL/GUI libraries will probably never be in the language and it has taken years for them to look at threads).
For these reasons, C++ has more emphasis on commercial and community libraries. Probably the most well known is Boost. It is a community driven library (anyone can have their code reviewed for inclusion and anyone can participate in the reviews). Some of the libraries in Boost are to prove the designs and are eventually added to C++ (Boost::Regex is an example of this). Other libraries are implementations of policies that may be in heavy use but will most likely never be in the language (SOCI and GIL).
API
How do I use the API
Boost mostly has good documentation, located at Boost website.
Approved Boost Libraries
Compiling Your Code
Installing Boost
Luckily Boost is included on the CS machines and is used in a lot of C++ Dev Shops, so you most likely will not have to install it. If you do need to install it, please use your distributions package manager (Linux) or go to the boost website (Windows/Mac OS)
Linking to Boost
Another area that you are in luck with. Most of Boost (due to templates) is header-only. If you used a feature that requires linking, all you need to do is
g++ ... -lboost_LIBRARY_NAME ...
For example:
g++ -g -Wall -o program code.cpp -lboost_regex
Example
boost_example.cpp (Download)
#include <iostream>
#include <boost/regex.hpp>
#include <boost/cstdlib.hpp>
int main (int argc, char* argv[])
{
bool found = false;
try
{
std::string buffer;
for (int i =0; std::getline(std::cin, buffer); ++i)
{
std::string pattern = "Hello World";
static const boost::regex exp (pattern);
if (boost::regex_match (buffer, exp))
{
found = true;
std::cout << "Found " << pattern << " on line " << i << std::endl;
}
}
}
catch (std::exception &e)
{
std::cout << "Failure: " << e.what() << std::endl;
}
catch (...)
{
std::cout << "Unknown Failure" << std::endl;
}
return found? boost::exit_success : boost::exit_failure;
}
To run this program, you can
-bash-3.2$ g++ -Wall -g boost_example.cpp -o example -lboost_regex -bash-3.2$ ./example The Hello World Found Hello World on line 1 he thetheHelloWorld