标签:style blog http color java io ar for art
今天发现 for each 语法居然可以直接编译通过,之前还以为只有开了/clr才可以支持。查了一下资料发现ms从vs2005就已经支持了。虽然不符合标准不过用着确实方便啊,必须记录一下。
具体看这里,已经有人介绍过了。http://www.codeproject.com/Tips/76166/For-each-loop-in-Native-C
Since Visual Studio 2005, native C++ has had a ‘for each’ loop construct, like C# or Java. Unfortunately it is restricted to collections from the STL library, such as vector. However, it does mean you can write very neat code to iterate through such a collection: vector<int> data(3); data[0] = 10; data[1] = 20; data[2] = 30; //instead of this int total = 0; for (vector<int>::iterator vi = data.begin(); vi != data.end(); vi++) { int i = *vi; total += i; } cout << "total: " << total << endl; // do this: total = 0; for each( const int i in data ) total += i; cout << "total: " << total << endl; Now we just need that making part of the C++ standard! If you are writing standard compliant code you will have to use the for_each function [^].
标签:style blog http color java io ar for art
原文地址:http://www.cnblogs.com/cartler/p/3944351.html