User Tools

Site Tools


cppcookbook

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
cppcookbook [2012/04/11 00:05]
mantis [Example: creating a lookup table for flags and full names]
cppcookbook [2012/04/11 09:42] (current)
mantis
Line 1: Line 1:
 +
 +
 +====== Loops ======
 +===== Loop over a Sequence =====
 +<code cpp>
 +#include <​boost/​foreach.hpp>​ vector<​string>​ ids; 
 +
 +BOOST_FOREACH(string id, ids) {
 +
 +// do something with id
 +
 +}
 +</​code>​
 +
 +===== Loop over a Mapping =====
 +<code cpp>
 +#include <​boost/​foreach.hpp> ​
 +#include <​boost/​tuple/​tuple.hpp> ​
 +
 +std::​map<​int,​ std::​string>​ theMap;  ​
 +int key;  ​
 +std::string value;  ​
 +
 +BOOST_FOREACH(boost::​tuples::​tie(key,​ value), theMap) {
 +
 +std::cout << key << " => " << value << "​\n";​
 +
 +}
 +</​code>​
 +
 +====== Strings ======
 +===== Split a string =====
 +<code cpp>
 +#include <​boost/​algorithm/​string.hpp>​
 +vector<​string>​ splitResult;​
 +boost::​split(splitResult,"​wir;​funktionieren;​automatik;;",​boost::​is_any_of(";,​."​));​ // results in |wir|funktionieren|automatik|
 +</​code>​
 +
 +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 =====
 +<code cpp>
 +#include <​boost/​algorithm/​string.hpp> ​
 +
 +vector<​string>​ data_bits; ​
 +string data_copy = boost::​join(data_bits,","​);​
 +</​code>​
 +
 +===== Lexical cast =====
 +<​code ​ cpp>
 +#include <​boost/​lexical_cast.hpp>​ int i = boost::​lexical_cast<​int>​("​2"​);​
 +</​code>​
 +
 +====== 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 ====
 +<code cpp>
 +vector<​int>​ a,b;
 +a.insert(a.end(),​b.begin(),​b.end());​
 +</​code>​
 +
 +===== 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...
 +
 +<code cpp>
 +#include <​boost/​multi_index_container.hpp>​
 +#include <​boost/​multi_index/​member.hpp>​
 +#include <​boost/​multi_index/​sequenced_index.hpp>​
 +#include <​boost/​multi_index/​ordered_index.hpp>​
 +
 +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<​flag>, ​ BOOST_MULTI_INDEX_MEMBER(FlagInfo,​std::​string,​flag)>​
 +  >
 + > FlagList;
 +// map/​list-like container
 +
 +typedef FlagList::​iterator FlagListIterator;​
 +</​code>​
 +
 +==== Insert some elements ====
 +<code cpp>
 +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
 +</​code>​
 +
 +==== Find ====
 +<code cpp>
 +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;
 +}
 +</​code>​
  
cppcookbook.txt ยท Last modified: 2012/04/11 09:42 by mantis