Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对于GCC和GCC版本中的每个_C++_Gcc_Foreach_C++11 - Fatal编程技术网

C++ 对于GCC和GCC版本中的每个

C++ 对于GCC和GCC版本中的每个,c++,gcc,foreach,c++11,C++,Gcc,Foreach,C++11,如何为GCC中的每个循环使用 我如何获得GCC版本?(在代码中)使用lambda,例如 // C++0x only. std::for_each(theContainer.begin(), theContainer.end(), [](someType x) { // do stuff with x. }); 从4.6开始,GCC就支持 // C++0x only for (auto x : theContainer) { // do stuff with x. } 该语法是M

如何为GCC中的每个循环使用

我如何获得GCC版本?(在代码中)

使用lambda,例如

// C++0x only.
std::for_each(theContainer.begin(), theContainer.end(), [](someType x) {
    // do stuff with x.
});
从4.6开始,GCC就支持

// C++0x only
for (auto x : theContainer) {
   // do stuff with x.
}
该语法是MSVC扩展。它在其他编译器中不可用

// MSVC only
for each (auto x in theContainer) {
  // do stuff with x.
}
但是你可以用它。它是可移植的,不需要C++0x也可以使用

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


请参阅如何获取GCC版本。

还有一种传统方法,不使用C++0X lambda。< C++ >标准函数的列表>使用函数和lambdas。< /p>我看不到这两个问题之间的任何关系……不,以C++ 0x格式。就像VC++ 2010一样:对于每个(自动的在VEC){CUT@用户:<代码>对于每个< /代码>,对于每个,还是它是原始C++的扩展?@ Jordnes STD::FoYYORY(,,)目前是ISO标准C++的一部分,定义在2003。它不是一个扩展,它是C++中当前可用的特性的一部分。必须包括<代码> <代码>标题。GCC 4.6是。
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);
}