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

当我尝试使用 std::stoi 并尝试编译它时,我收到错误消息stoi 不是 std 的成员".我正在从命令行使用 g++ 4.7.2,所以它不会是 IDE 错误,我按顺序排列了所有包含,并且 g++4.7.2 默认使用 c++11.如果有帮助,我的操作系统是 Ubuntu 12.10.有什么我没有配置的吗?

I get the error message "stoi is not a member of std" when I try to use std::stoi and try to compile it. I'm using g++ 4.7.2 from the command line so it can't be IDE error, I have all my includes in order, and g++4.7.2 defaults to using c++11. If it helps, my OS is Ubuntu 12.10. Is there something I haven't configured?

#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;
}

不会编译.不过也没什么不好.

Will not compile. But there's nothing wrong with it.

推荐答案

std::stoi() 是 C++11 所以你必须确保你编译它:

std::stoi() is new in C++11 so you have to make sure you compile it with:

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

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

相关文章