首页 > 代码库 > For each loop in Native C++
For each loop in Native C++
今天发现 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 thisint 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 [^].
For each loop in Native C++
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。