如何在最新版本的 g++ 中使用 C++ 11 特性

Pig*_*gna 6 upgrade c++ g++

新手来了 我刚从终端运行我编写的 C++ 程序时出错:error: ‘stoi’ is not a member of ‘std’. 他们告诉我编译器太旧了。

我正在使用 Ubuntu 14.04。

我的 g++ 版本是 4.8.4。

如何升级?

mur*_*uru 5

你不需要升级。将标准版本指定为g++. 例如,要从 cppreference.com编译示例程序

$ g++ --version
g++ (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --std=c++11 -o test test.cpp
$ ./test
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
Run Code Online (Sandbox Code Playgroud)

  • @Pigna 如果你经常运行 `g++`,你应该[编写 Makefile](http://stackoverflow.com/a/16886737/2072269)。 (2认同)