For each version of GCC and GCC

How can I use for each cycle in GCC?

and how can i get the gcc version? (in code)

+3
source share
2 answers

Use lambda like

// C++0x only.
std::for_each(theContainer.begin(), theContainer.end(), [](someType x) {
    // do stuff with x.
});

range-based for cycle supported by GCC since 4.6.

// C++0x only
for (auto x : theContainer) {
   // do stuff with x.
}

for each loop syntax is an extension of MSVC. It is not available in other compilers.

// MSVC only
for each (auto x in theContainer) {
  // do stuff with x.
}

But you could just use Boost.Foreach . It is portable and available without C ++ 0x.

// Requires Boost
BOOST_FOREACH(someType x, theContainer) {
  // do stuff with x.
}

See How to check the current version of GCC? on how to get the GCC version.

+20
source

, ++ 0X . <algorithm> , . (++ 0x lambdas , ())

struct Functor
{
   void operator()(MyType& object)
   {
      // what you want to do on objects
   }
}

void Foo(std::vector<MyType>& vector)
{
  Functor functor;
  std::for_each(vector.begin(), vector.end(), functor);
}

. ++, .

+6

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


All Articles