zoukankan      html  css  js  c++  java
  • 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 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 [^].
  • 相关阅读:
    C# DES加密解密
    VC SOCKET 压缩通信学习
    ASPX一句话爆破工具
    VC读取文件内容
    VC查找字符串
    (学习记录)代码注入之远程线程篇
    WINSOCK 传送文件
    VC数据类型
    占用字节数求法
    HDU
  • 原文地址:https://www.cnblogs.com/cartler/p/3944351.html
Copyright © 2011-2022 走看看