C++ > Boost

Conversions and String manipulation

int to std::string

int a = 10;
string str = (boost::format("%1%") % a).str();

Find the type of an object (demangled)

#include <boost/core/demangle.hpp>
 
std::string type(boost::core::demangle(typeid(object).name()));
cout << "object is of type " << type;

Data structures

std::set<int> theSet;
BOOST_FOREACH(int theValue, theSet) {
    cout << theValue<< endl;
}
typedef std::map<int, int> map_t;
map_t mmap;  
BOOST_FOREACH(map_t::value_type &i, mmap) {
    cout << i.first << " = " << i.second;
}

boost::tuple

#include <boost/tuple/tuple.hpp>
 
boost::tuple<std::string, int, bool> tuple = boost::make_tuple(std::string("this"), 15, true);
std::string chaine = tuple.get<0>();
int entier = tuple.get<1>();
bool booleen = tuple.get<2>();

Other

Obtain pointer behind shared_ptr

boost::shared_ptr<Foo> foo_ptr(new Foo());
Foo* raw_foo = foo_ptr.get();

Create folder

#include <boost/filesystem.hpp>
 
boost::filesystem::path createDir(std::string name) {
	boost::filesystem::path dir(name);
	if (boost::filesystem::create_directory(dir)) {
		return dir;
	} else {
		throw RequestException("Unable to create folder " + dir.native() + " !");
	}
}

Generate UUID

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
 
std::stringstream ss;
ss << boost::uuids::random_generator()();
return ss.str();
Print/export