簡體   English   中英

g ++ 4.7.2中缺少std :: stoi嗎?

[英]std::stoi missing in g++ 4.7.2?

當我嘗試使用std :: stoi並嘗試對其進行編譯時,出現錯誤消息“ stoi不是std的成員”。 我從命令行使用g ++ 4.7.2,因此不會出現IDE錯誤,我的命令全部包含在內,並且g ++ 4.7.2默認使用c ++ 11。 如果有幫助,我的操作系統是Ubuntu 12.10。 有沒有我沒有配置的東西嗎?

#include <iostream>
#include <string>

using namespace std;

int main(){
  string theAnswer = "42";
  int ans = std::stoi(theAnswer, 0, 10);

  cout << "The answer to everything is " << ans << endl;
}

不會編譯。 但是,這沒有錯。

std::stoi()C ++ 11中的新增功能,因此您必須確保使用以下命令對其進行編譯:

g++ -std=c++11 example.cpp

要么

g++ -std=c++0x example.cpp

對於較舊的C ++版本,編譯器不支持stoi。 對於較舊的版本,您可以使用以下代碼段將字符串轉換為整數。

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    string input;
    cin >> input;
    int s = std::atoi(input.c_str());
    cout<<s<<endl;
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM