Using Boost adaptors with C++11 lambdas

http://smellegantcode.wordpress.com/2011/10/31/linq-to-c-or-something-much-better/ But you can use this, that works well. #include <boost/range/adaptors.hpp> #include <boost/range/algorithm.hpp> #include <vector> #include <functional> int main() { std::vector<int> v{ 1,5,4,2,8,5,3,7,9 }; std::function<int(int)> func = [](int i) { return -i; }; std::cout << *boost::min_element(v | boost::adaptors::transformed( func)) << std::endl; return 0; } http://liveworkspace.org/code/b78b3f7d05049515ac207e0c12054c70 #define BOOST_RESULT_OF_USE_DECLTYPE works fine in VS2012 for example.

How do I “normalize” a pathname using boost::filesystem?

Boost v1.48 and above You can use boost::filesystem::canonical: path canonical(const path& p, const path& base = current_path()); path canonical(const path& p, system::error_code& ec); path canonical(const path& p, const path& base, system::error_code& ec); http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonical v1.48 and above also provide the boost::filesystem::read_symlink function for resolving symbolic links. Boost versions prior to v1.48 As mentioned in other answers, … Read more

Conversion from boost::shared_ptr to std::shared_ptr?

Based on janm’s response at first I did this: template<class T> std::shared_ptr<T> to_std(const boost::shared_ptr<T> &p) { return std::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } template<class T> boost::shared_ptr<T> to_boost(const std::shared_ptr<T> &p) { return boost::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } But then I realized I could do this instead: namespace { template<class SharedPointer> struct Holder { … Read more

How to enable_shared_from_this of both parent and derived

The OP solution can be made more convenient by defining the following on the base class. protected: template <typename Derived> std::shared_ptr<Derived> shared_from_base() { return std::static_pointer_cast<Derived>(shared_from_this()); } This can be made more convenient by placing it in a base class (for reuse). #include <memory> template <class Base> class enable_shared_from_base : public std::enable_shared_from_this<Base> { protected: template <class … Read more

Calculate mean and standard deviation from a vector of samples in C++ using Boost

I don’t know if Boost has more specific functions, but you can do it with the standard library. Given std::vector<double> v, this is the naive way: #include <numeric> double sum = std::accumulate(v.begin(), v.end(), 0.0); double mean = sum / v.size(); double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0); double stdev = std::sqrt(sq_sum / v.size() – mean … Read more

Why does Boost.Asio not support an event-based interface?

Boost.Asio is a C++ library for network and low-level I/O programming. As such, OS-level synchronization objects, such as condition variables, are outside of the scope of the library, and a much better fit for Boost.Thread. The Boost.Asio author often presents the boost::asio::io_service as the bridge or link between the application and the OS. While this … Read more

Boost::Spirit::QI parser: index of parsed element

There are a number of approaches. What I’d usually recommend instead, is using well thought out repeat(n) expressions with directly exposed container attributes (like vector<vector<double> >). What you seem to be looking for is semantic actions with state. (This is common practice coming from lex/yacc). I treat these approaches in three full demos below (1., … Read more

Remove 100,000+ nodes from a Boost graph

In your removal branch you re-tie() the iterators: boost::tie(vi, vi_end) = boost::vertices(m_graph); This will cause the loop to restart every time you restart the loop. This is exactly Schlemiel The Painter. I’ll find out whether you can trust remove_vertex not triggering a reallocation. If so, it’s easily fixed. Otherwise, you’d want an indexer-based loop instead … Read more

Strange exception throw – assign: Operation not permitted

Boost.Asio’s POSIX stream-oriented descriptors explicitly do not support regular files. Hence, if test is a regular file, then ./client < test will result in posix::stream_descriptor::assign() failing when attempting to assign STDIN_FILENO to the stream_descriptor. The documentation states: Boost.Asio includes classes added to permit synchronous and asynchronous read and write operations to be performed on POSIX … Read more