Sorting zipped (locked) containers in C++ using boost or the STL

Here’s a working example based on the range-v3 Library that has been proposed for Standardization #include <range/v3/all.hpp> #include <iostream> using namespace ranges; int main() { std::vector<int> a1{15, 7, 3, 5}; std::vector<int> a2{ 1, 2, 6, 21}; sort(view::zip(a1, a2), std::less<>{}, &std::pair<int, int>::first); std::cout << view::all(a1) << ‘\n’; std::cout << view::all(a2) << ‘\n’; } Live Example (requires … Read more

What is the performance overhead of std::function?

There are, indeed, performance issues with std:function that must be taken into account whenever using it. The main strength of std::function, namely, its type-erasure mechanism, does not come for free, and we might (but not necessarily must) pay a price for that. std::function is a template class that wraps callable types. However, it is not … Read more

How do I sort a std::vector by the values of a different std::vector?

friol’s approach is good when coupled with yours. First, build a vector consisting of the numbers 1…n, along with the elements from the vector dictating the sorting order: typedef vector<int>::const_iterator myiter; vector<pair<size_t, myiter> > order(Index.size()); size_t n = 0; for (myiter it = Index.begin(); it != Index.end(); ++it, ++n) order[n] = make_pair(n, it); Now you … Read more

Example for boost shared_mutex (multiple reads/one write)?

1800 INFORMATION is more or less correct, but there are a few issues I wanted to correct. boost::shared_mutex _access; void reader() { boost::shared_lock< boost::shared_mutex > lock(_access); // do work here, without anyone having exclusive access } void conditional_writer() { boost::upgrade_lock< boost::shared_mutex > lock(_access); // do work here, without anyone having exclusive access if (something) { … Read more

Weighted random numbers

There is a straightforward algorithm for picking an item at random, where items have individual weights: 1) calculate the sum of all the weights 2) pick a random number that is 0 or greater and is less than the sum of the weights 3) go through the items one at a time, subtracting their weight … Read more

How to use Boost in Visual Studio 2010

While Nate’s answer is pretty good already, I’m going to expand on it more specifically for Visual Studio 2010 as requested, and include information on compiling in the various optional components which requires external libraries. If you are using headers only libraries, then all you need to do is to unarchive the boost download and … Read more

Why do I need strand per connection when using boost::asio?

The documentation is correct. With a half duplex protocol implementation, such as HTTP Server 3, the strand is not necessary. The call chains can be illustrated as follows: void connection::start() { socket.async_receive_from(…, &handle_read); —-. } | .————————————————‘ | .—————————————–. V V | void connection::handle_read(…) | { | if (result) | boost::asio::async_write(…, &handle_write); —|–. else if … Read more

Boost spirit skipper issues

In general the following directives are helpful for inhibiting/switching skippers mid-grammar: qi::lexeme [ p ]which inhibits a skipper, e.g. if you want to be sure you parse an identifier without internal skips) – see also no_skip for comparison qi::raw [ p ]which parses like always, including skips, but returns the raw iterator range of the … Read more