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

时间:2013-02-07 05:02:55

标签: c++ compiler-errors g++ std c++-standard-library

当我尝试使用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;
}

不会编译。但它并没有错。

2 个答案:

答案 0 :(得分:16)

std::stoi()C++11中的新功能,因此您必须确保使用以下代码进行编译:

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

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

答案 1 :(得分:4)

对于旧版本的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;
}
相关问题