STL algorithm copy if with functor

Khurshid

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.

Praetorian

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing dynamic functor to stl

From Dev

STL criterion for which algorithm have a copy version and which have not?

From Dev

How to work with stl with functor wrap of function pointer?

From Dev

STL algorithm for Vector Add

From Dev

STL algorithm/functional

From Dev

Reverse Iteration of an STL Algorithm

From Dev

STL algorithm/functional

From Dev

Error with custom iterator and STL Algorithm

From Dev

Passing STL algorithm to another function

From Dev

Expected return of find STL algorithm

From Dev

Using function object in stl algorithm

From Dev

Efficiency of STL's copy function

From Dev

STL Algorithms to generate and copy a vector

From Dev

What does it take to be an `stl` algorithm compatible container?

From Dev

What is the return type of the STL algorithm "count", on a valarray

From Java

Sum two vector and store stl algorithm

From Dev

STL Algorithm element wise multiplication WITHOUT sum

From Dev

STL Algorithm that Takes a Test and Mutate Function

From Dev

How does an STL algorithm identify the container?

From Dev

STL Algorithm that Takes a Test and Mutate Function

From Dev

How does an STL algorithm identify the container?

From Dev

Adapt std::pair for STL algorithm lambda

From Dev

How to copy vector to map in STL in a graceful way

From Dev

C++ STL's copy() exception safety

From Dev

Thrust to STL copy doesn't work as intended

From Dev

Xcode - shallow copy in STL string assignment

From Dev

Unnecessary object copy - C++ STL

From Dev

Xcode - shallow copy in STL string assignment

From Dev

Thrust to STL copy doesn't work as intended