This can be done using Boost by creating a zip_iterator
and then iterating over the tuple
of iterators from both containers in parallel.
First pass a pair of zip_iterators
to std::remove_if
, and have the predicate inspect the elements of the first vector
for NaN
auto result = std::remove_if(boost::make_zip_iterator(boost::make_tuple(v1.begin(), v2.begin())),
boost::make_zip_iterator(boost::make_tuple(v1.end(), v2.end())),
[](boost::tuple<double, int> const& elem) {
return std::isnan(boost::get<0>(elem));
});
Then use vector::erase
to remove the unneeded elements.
v1.erase(boost::get<0>(result.get_iterator_tuple()), v1.end());
v2.erase(boost::get<1>(result.get_iterator_tuple()), v2.end());
Live demo
The boilerplate required to create the zipped iterator ranges can be further reduced by using boost::combine
and Boost.Range's version of remove_if
.
auto result = boost::remove_if(boost::combine(v1, v2),
[](boost::tuple<double, int> const& elem) {
return std::isnan(boost::get<0>(elem));
});
Live demo
std::vector<std::pair<int,int>>
in the first place – Ratan