2013-09-23 30 views
1
#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中自动更改含义”,另一个是“!=操作符未定义”。之前从未有过这个问题。“在C++ 11中自动更改含义”

我只使用自动操作符,因为本书建议。

我是初学者,约2个月后重新学习。 遇到问题赶上。

+6

编译与'-std = C++ 11'。另外,这可以通过'std :: transform'或者ranged for循环来完成。 – chris

+0

编译器启用了C++ 11。 – Slay

+2

然后得到一个较新的版本,我想。 – chris

回答

7

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

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

2

正如克里斯提到的,使用基于范围的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; 
    } 

- 赛义德Amrollahi Boyouki