简体   繁体   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;
}

There are two errors I get. 我得到两个错误。 One is as mentioned, "auto changes meaning in c++11" and the other is "!= operator not defined." 如前所述,一个是“自动更改c ++ 11中的含义”,另一个是“!=运算符未定义”。 Never had this problem before. 从来没有过这个问题。

I only used the auto operator because the book suggested. 我只使用自动运算符,因为这本书建议这样做。

I'm a beginner, and getting back to learning after about 2 months. 我是一个初学者,大约两个月后重新开始学习。 Having trouble catching up. 赶上时遇到麻烦。

Your code runs ok when compiled with -std=c++11 , You may check it here . 使用-std=c++11编译时,您的代码可以正常运行,您可以在此处进行检查。

You can add the option in Setting->Compiler->Have g++ follow the C++11 ISO C++ language standard [-std=C++11] in CodeBlocks. 您可以在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]中添加该选项。

As chris mentioned, using Range-based for loop is much better. 正如克里斯所说,使用基于范围的for循环要好得多。 It's closer to spirit of C++11 and it's easier to learn for beginners. 它更接近C ++ 11的精神,对于初学者来说更容易学习。 Consider: 考虑:

    #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 -赛义德(Saeed Amrollahi)Boyouki

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

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