Answers for "c++ foreach vector"

C++
1

c++ vector foreach

std::vector<Entity> entities;

for (Entity& entity : entities) {
	entity.doStuff();                
}
Posted by: Guest on August-11-2021
0

c++ foreach vector

for(int i=0; i < size(vector); i++){
   vector[i]=0;
}
Posted by: Guest on April-07-2021
2

for each c++

// for_each example
#include <iostream>     // std::cout
#include <algorithm>    // std::for_each
#include <vector>       // std::vector

void myfunction (int i) {  // function:
  std::cout << ' ' << i;
}

struct myclass {           // function object type:
  void operator() (int i) {std::cout << ' ' << i;}
} myobject;

int main () {
  std::vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);

  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myfunction);
  std::cout << '\n';

  // or:
  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myobject);
  std::cout << '\n';

  return 0;
}
Posted by: Guest on April-20-2020
0

for each c++

template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function fn)
{
  while (first!=last) {
    fn (*first);
    ++first;
  }
  return fn;      // or, since C++11: return move(fn);
}
Posted by: Guest on April-20-2020

Browse Popular Code Answers by Language