More out of interest than anything else, I wrote a basic wrapper that can be used for copy_if . This is similar to std::back_inserter , but instead of just pasting an object, it inserts std::get<N>(obj) . This makes it suitable for std::array , std::tuple and std::pair .
To insert pair.first , as we want in this example, we use back_n_inserter<0> .
template< std::size_t N, class Container > class back_n_insert_iterator : public std::iterator< std::output_iterator_tag, void, void, void, void > { public: back_n_insert_iterator (Container& c) : c{c} {};
Now we can use our adapter in the copy_if call:
std::copy_if(theMap.begin(), theMap.end(), back_n_inserter<0>(arrKeys), [&value](const auto& el){return el.second == value;});
Thanks to @dyp for all the suggestions.
source share