Visual Studio 的 C++ 从 VC 2005 开始支持了类似 Java, C# 的 "for each" 语法了!
以后在 C++ 的代码中我们可以写以下这样的代码了!
include "stdafx.h" #include <vector> #include <iostream>
int main( int argc, char* argv[] ) { std::vector< int > data( 5 );
data[ 0 ] = 0; data[ 1 ] = 1; data[ 2 ] = 2; data[ 3 ] = 3; data[ 4 ] = 4;
int total = 0; for ( std::vector< int >::iterator it = data.begin(); it != data.end(); it++ ) { int value = *it; total += value; }
std::cout << total << std::endl;
total = 0; for each ( const int i in data ) { total += i; }
std::cout << total << std::endl;
return 0; }
|
阅读(392) | 评论(0) | 转发(0) |