【问题标题】:"auto changes meaning in c++11"“自动更改 C++11 中的含义”
【发布时间】:2013-09-27 19:39:55
【问题描述】:
#include<iostream>
#include<string>
#include<iterator>
using namespace std;
int main()
{
    string a("hello world");
    for(auto it = a.begin(); it != a.end() && !isspace(*it); it++ )
    {
        *it = toupper(*it);
    }
    cout<<a;
}

我得到两个错误。一个如前所述,“自动更改 c++11 中的含义”,另一个是“!= 未定义运算符”。以前没遇到过这个问题。

因为书上的建议,我只使用自动运算符。

我是初学者,大约 2 个月后开始学习。 赶不上。

【问题讨论】:

  • 使用-std=c++11 编译。此外,最好通过std::transform 或范围 for 循环来完成。
  • 编译器启用了c++11。
  • 那我想换个新版本吧。
  • “自动更改含义”真的是错误还是只是信息警告?
  • 将警告信息添加到问题中,而不是在评论中发布。

标签: c++ c++11 iterator operators


【解决方案1】:

正如克里斯所说,使用基于范围的 for 循环要好得多。更接近 C++11 的精神 并且对于初学者来说更容易学习。考虑:

    #include <string>
    #include <iostream>
    int main()
    {
       std::string s{"hello, world"}; // uniform initialization syntax is better
       for (auto& c : s)  // remember to use auto&
         if (!isspace(c)) c = toupper(c);
       cout << s << '\n'; // remember the last newline character  
       return 0;
    }

-- Saeed Amrollahi Boyouki

【讨论】:

    【解决方案2】:

    您的代码在使用-std=c++11 编译时运行正常,您可以检查它here

    您可以在 CodeBlocks 中添加Setting-&gt;Compiler-&gt;Have g++ follow the C++11 ISO C++ language standard [-std=C++11] 中的选项。

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多