Which algorithm or combine of algorithms can use for following situation?
struct Term
{
int ix;
double f;
};
std::vector<Term> terms = <intitalize terms>;
std::vector< int > termIxVector;
// NEED get all `ix` from the `terms` where term.f < 1.0,
// and insert 'ix' result to termIxVector.
//i.e. equavalent this loop:
for(std::size_t i = 0; i < terms.size(); ++i)
if ( terms[i].f < 1.0 )
termIxVector.push_back(terms[i].ix);
std::copy_if copies only Term structure. std::transform - doesn't support predicate.
Use std::for_each
with a lambda
std::for_each(terms.begin(), terms.end(),
[&termIxVector](Term const& t) {
if(t.f < 1.0) termIxVector.push_back(t.ix);
});
Or a range based for
for(auto const& t : terms) {
if(t.f < 1.0) termIxVector.push_back(t.ix);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments