====== Loops ======
===== Loop over a Sequence =====
#include vector ids;
BOOST_FOREACH(string id, ids) {
// do something with id
}
===== Loop over a Mapping =====
#include
#include
std::map theMap;
int key;
std::string value;
BOOST_FOREACH(boost::tuples::tie(key, value), theMap) {
std::cout << key << " => " << value << "\n";
}
====== Strings ======
===== Split a string =====
#include
vector splitResult;
boost::split(splitResult,"wir;funktionieren;automatik;;",boost::is_any_of(";,.")); // results in |wir|funktionieren|automatik|
Note Boost's split behaviour where two separators meet in a string: split creates an empty element to signal there's no data between those two separators. Thus it's possible to reconstruct the original string.
===== Join a vector of strings =====
#include
vector data_bits;
string data_copy = boost::join(data_bits,",");
===== Lexical cast =====
#include int i = boost::lexical_cast("2");
====== Filesystem ======
===== Finding a file =====
[[http://www.boost.org/doc/libs/1_35_0/libs/filesystem/doc/index.htm TODO]]
====== Containers ======
===== STL-Vector =====
==== Append vector to other vector ====
vector a,b;
a.insert(a.end(),b.begin(),b.end());
===== BOOST-List/map hybrid =====
Sometimes a container with fast access possibilities AND insertion order preserving capability is needed. Boost's multi_index_container helps (see [[http://docs.huihoo.com/boost/1-33-1/libs/multi_index/doc/tutorial.html#seq_indices here for more]]): Use a sequenced index together with oder indizes of your choice.
==== Example: creating a lookup table for flags and full names ====
f -> fleetid
t -> timestamp...
#include
#include
#include
#include
namespace bmi = boost::multi_index;
struct FlagInfo {
std::string flag; // eg. F
std::string full_name; // eg. fleetid
FlagInfo(std::string f,std::string n):flag(f),full_name(n) {}
};
struct flag{}; // to access the tag
/** a map/list hybridcontainer preserving insertion order and indexing by flag*/
typedef bmi::multi_index_container<
FlagInfo,
bmi::indexed_by<
bmi::sequenced<>,
bmi::ordered_unique< bmi::tag, BOOST_MULTI_INDEX_MEMBER(FlagInfo,std::string,flag)>
>
> FlagList;
// map/list-like container
typedef FlagList::iterator FlagListIterator;
==== Insert some elements ====
FlagList flag_list;
flag_list.get<1>().insert(FlagInfo("o","orthdist")); // use index 1 of FlagInfo container
flag_list.get<1>().insert(FlagInfo("f","fleetid")); // use index 1 of FlagInfo container
==== Find ====
FlagList::nth_index<1>::type::iterator itr = flag_list.get<1>().find(flag); // use first index
if (itr != flag_list.get<1>().end()) {
cout << itr->full_name << endl;
}