gpt4 book ai didi

c++ - 为什么我无法使用 C++11 基于范围的 for 循环迭代 vector ?

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:21 27 4
gpt4 key购买 nike

std::vector<std::string> endpointLSRKey;

for(auto it = endpointLSRKey.begin(); it != endpointLSRKey.end(); ++it) {
lsr->setItem(0, "", *it, address);
}

然后得到一个错误说“auto”已经改变了c++11中的含义,删除它!

所以我改成这样:

for(std::string& str : endpointLSRKey){
lsr->setItem(0, "", str, address);
}

然后得到一个错误,说“在 C++98 模式下不允许基于范围的‘for’循环”

一头雾水,怎么迭代呢??

为什么有时是 c++11 而有时是 c++98?

最佳答案

听起来您需要将编译器设置为 C++11 模式。错误信息:

"range-based 'for' loops are not allowed in C++98 mode"

指出这一点。

执行此操作的方式取决于您的编译器。

当您看到此消息时:

get an error says that "auto" has changed the meanings in c++11

您的编译器仍处于 C++98/03 模式。 C++03中有关键字auto,但在大多数情况下它没有任何意义,因为默认情况下变量会进入自动存储。 (您可以通过使用 static 标记变量使其进入静态存储。也就是说,autostatic as signedunsigned(在 C++98/03 中)。

因为这个含义在 C++98/03 中并不是很有用,并且因为 C++11 中的含义发生了变化,C++03 模式下的编译器可能会提示使用 auto 带有警告。如果您将警告视为错误,那么它就会变成错误。因此,您看到的消息。

关于c++ - 为什么我无法使用 C++11 基于范围的 for 循环迭代 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243463/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com