std::tuple get() member function

From C++0x draft: [ Note: The reason get is a nonmember function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword. — end note ] This can be illustrated with this code: template <typename T> struct … Read more

Creating JSON arrays in Boost using Property Trees

Simple Array: #include <boost/property_tree/ptree.hpp> using boost::property_tree::ptree; ptree pt; ptree children; ptree child1, child2, child3; child1.put(“”, 1); child2.put(“”, 2); child3.put(“”, 3); children.push_back(std::make_pair(“”, child1)); children.push_back(std::make_pair(“”, child2)); children.push_back(std::make_pair(“”, child3)); pt.add_child(“MyArray”, children); write_json(“test1.json”, pt); results in: { “MyArray”: [ “1”, “2”, “3” ] } Array over Objects: ptree pt; ptree children; ptree child1, child2, child3; child1.put(“childkeyA”, 1); child1.put(“childkeyB”, 2); … Read more

Example to use shared_ptr?

Using a vector of shared_ptr removes the possibility of leaking memory because you forgot to walk the vector and call delete on each element. Let’s walk through a slightly modified version of the example line-by-line. typedef boost::shared_ptr<gate> gate_ptr; Create an alias for the shared pointer type. This avoids the ugliness in the C++ language that … Read more

BOOST ASIO – How to write console server

The problem is: How can I attach (or write) console, which can calls above functionalities. This console have to be a client? Should I run this console client as a sepearate thread? You don’t need a separate thread, use a posix::stream_descriptor and assign STDIN_FILENO to it. Use async_read and handle the requests in the read … Read more

Cut set of a graph, Boost Graph Library

Here’s a quick PoC based on Boost BiMap typedef boost::bimap<bimaps::list_of<default_color_type>, bimaps::set_of<Traits::vertex_descriptor> > smart_map; smart_map colorMap; boost::associative_property_map<smart_map::right_map> color_map(colorMap.right); I’ve taken a small sample from http://lpsolve.sourceforge.net/5.5/DIMACS_maxf.htm and you can see it Live On Coliru, output: c The total flow: s 15 c flow values: f 0 1 5 f 0 2 10 f 1 3 5 f … Read more

want to efficiently overcome mismatch between key types in a map in Boost.Interprocess shared memory

You can use a custom comparator struct MyLess { template <typename T, typename U> bool operator()(const T&t, const U&u) const { return t<u; } }; In your code you can just typedef it as StringComparator UPDATE To the comments Multi Index To The Rescue If you want to replace the std::map/boost::container::map with a Boost Multi … Read more