[英]“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.