Foreach algorithm in c ++

Is there a way to get the return value from the function that I am passing in foreach.

For example: I have

void myfunction (int i) 
{
        cout << " " << i;
}

vector<int> myvector;
myvector.push_back(10);
for_each (myvector.begin(), myvector.end(), myfunction);

Let's say I want to count the number of elements in a vector using some kind of rule, I want to get the return value from myFunction, is this possible?

+3
source share
10 answers

No. But you can make it a myfunctionfunctor, pass it a pointer to some memory, and save the return value through this pointer.

struct MyFunctor {
    int *count;
    MyFunctor(int *count_) : count(count_) { }
    void operator()(int n) {
        if (n > 5) (*count)++;
    }
};

int main() {
    vector<int> vec;
    for (int i=0; i<10; i++) vec.push_back(i);
    int count = 0;
    for_each(vec.begin(), vec.end(), Myfunctor(&count));
    printf("%d\n", count);
    return 0;
}

: , , for_each MyFunctor, . ; GMan, . , gcc (4.4.2). , , , <algorithm>.

+5

std::count ( ) std::count_if (count, true). std::for_each , .

+13

for_each , . , :

template <typename T>
class has_value
{
    has_value(const T& pValue) : mValue(pValue), mFlag(false) {}

    void operator()(const T& pX)
    {
        if (pX == mValue)
            mFlag = true;
    }

    operator bool(void) const { return mFlag; }
private:
    T mValue;
    bool mFlag;
};

bool has_seven = std::for_each(myvector.begin(), myvector.end(), has_value<int>(7));

. .., algorithm , . ( count)

+9

foreach, BOOST_FOREACH makro. , boost - , boost_foreach.hpp(afair). :

BOOST_FOREACH( int & i , my_vector )
{
     i = 0;
}

My_vector vector<int> int[] .

+3

, ?

+2

std::for_each . std::count , , std::count_if , :

std::vector<SomeType> vec;
std::count(vec.begin(), vec.end(), SomeType(9));
/*or */
bool myfunc(const SomeType& v)
{
    return v == 9;
}
std::count_if(vec.begin(), vec.end(), f);

ostream, std::cout, std:: copy:

std::vector<SomeType> vec;
...
std::copy(vec.begin(), vec.end(), \
    std::ostream_iterator<SomeType>(std::cout," "));

, std::transform:

std::vector<SomeType> src;
std::vector<SomeType> result;
int myfunc(int val)
{
    ...
}
std::transform(src.begin(), src.end() \
    result.begin(), myfunc);

std::transform , , .

+1

, , , ...

, for_each (count, accumulate, transform,...)

, : for_each , . , .

, for_each Predicate, , . ... , , , , .

class Predicate
{
public:
  Predicate(size_t& errors) : m_errors(errors) {}
  void operator()(MyObject& o)
  {
    try { /* complicated */ } catch(unfit&) { ++m_errors; }
  }
private:
  size_t& m_errors;
};

std::vector<MyObject> myVec;
// fill myVec

size_t errors = 0;
std::for_each(myVec.begin(), myVec.end(), Predicate(errors));

, size_t, .

+1

std::for_each , GMan.

.

std::count std::count_if, , , std::accumulate. .

std::transform , , .

+1

:

int main()
{
  std::vector<int> v;
  for (int i = 1; i <= 10; i++)
    v.push_back(i);

  int hits = 0;
  CountEven evens(&hits);
  std::for_each(v.begin(), v.end(), evens);
  std::cout << "hits = " << hits << std::endl;

  return 0;
}

CountEvens:

class CountEven {
  public:
  CountEven(int *hits) : hits(hits) {}
  CountEven(const CountEven &rhs) : hits(rhs.hits) {}
  void operator() (int n) { if (n % 2 == 0) ++*hits; }

  private:
  int *hits;
};

Note that the copy constructor forces multiple instances to share the same pointer.

Use std::countor std::count_if.

0
source

Source: https://habr.com/ru/post/1723931/


All Articles