繁体   English   中英

“自动更改c ++ 11中的含义”

[英]“auto changes meaning in c++11”

#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中的含义”,另一个是“!=运算符未定义”。 从来没有过这个问题。

我只使用自动运算符,因为这本书建议这样做。

我是一个初学者,大约两个月后重新开始学习。 赶上时遇到麻烦。

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

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

正如克里斯所说,使用基于范围的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM