Vscode移植到VS2010遇到的问题C++

如果在vscode能运行,就是C++版本的问题,VS2010仅支持部分C++11及以上的功能,仍然有一小部分不支持,但是他的警告信息和错误信息又很不明显,所以花了很多时间纠错。

总结:

看这三种for循环

 for (const Product& product : products) {
        if (product.type == TYPE_A) {
            sumA += product.price;
            countA += 1.0;
        } else if (product.type == TYPE_B) {
            sumB += product.price;
            countB += 1.0;
        }
    }

这段代码使用了 C++11 引入的范围-based for 循环,也称为 foreach 循环。在这个循环中,我们不需要通过迭代器或者索引来遍历容器,而是直接使用类似于数组的方式来遍历容器中的元素。这种语法使得代码更加简洁易读。在vs2010中会报错。

for (vector<Product>::const_iterator it = products.begin(); it != products.end(); ++it) {
    const Product& product = *it;
    if (product.type == TYPE_A) {
        sumA += product.price;
        countA += 1.0;
    } else if (product.type == TYPE_B) {
        sumB += product.price;
        countB += 1.0;
    }
}

这段代码使用了一个 vector<Product>::const_iterator 类型的迭代器 it 来遍历 products 容器。通过 it-> 访问迭代器指向的对象 ,在vs2010也会报错。

for (int i = 0; i < products.size(); ++i) {
    const Product& product = products[i];
    if (product.type == TYPE_A) {
        sumA += product.price;
        countA += 1.0;
    } else if (product.type == TYPE_B) {
        sumB += product.price;
        countB += 1.0;
    }
}

这种是最普通的 for 循环和索引 i 来遍历 products 容器。在循环体内,我们通过索引 i 访问容器中的元素,所以在vs2010适用。

相关推荐

  1. Vscode移植VS2010遇到问题C++

    2024-01-30 16:32:02       56 阅读
  2. LayaBox键盘控制移动遇到问题

    2024-01-30 16:32:02       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-30 16:32:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-30 16:32:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-30 16:32:02       82 阅读
  4. Python语言-面向对象

    2024-01-30 16:32:02       91 阅读

热门阅读

  1. C++发起Https请求

    2024-01-30 16:32:02       47 阅读
  2. Pull模式和Push模式

    2024-01-30 16:32:02       61 阅读
  3. 《zdppy_aocrud官方教程》 05 自动生成更新接口

    2024-01-30 16:32:02       49 阅读
  4. 算法训练营Day59(单调栈)

    2024-01-30 16:32:02       63 阅读
  5. 【算法】动态规划引入

    2024-01-30 16:32:02       59 阅读
  6. gitignore规则

    2024-01-30 16:32:02       59 阅读
  7. 生物科学大模型:驱动生物医学研究的未来

    2024-01-30 16:32:02       69 阅读
  8. 如何将本地项目上传到ac git 云端

    2024-01-30 16:32:02       69 阅读