码迷,mamicode.com
首页 > 编程语言 > 详细

For each loop in Native C++

时间:2014-08-29 10:59:17      阅读:243      评论:0      收藏:0      [点我收藏+]

标签: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 [^].

 

For each loop in Native C++

标签:style   blog   http   color   java   io   ar   for   art   

原文地址:http://www.cnblogs.com/cartler/p/3944351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!