如何在 gcc 中使用 C++11 std::stoi?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13589947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use C++11 std::stoi with gcc?
提问by Razorfever
Possible Duplicate:
How to convert a number to string and vice versa in C++
可能的重复:
如何在 C++ 中将数字转换为字符串,反之亦然
I am using Qt Creator 2.5.0and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
我正在使用 Qt Creator 2.5.0和 gcc 4.7(Debian 4.7.2-4)。我在 .pro 文件中添加了“QMAKE_CXXFLAGS += -std=c++11”。一切似乎都很好,我使用了 C++11 std::for_each 等等。但是,当我包含“字符串”标头并想使用 stoi 时,出现以下错误:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGWand one more, to Eclipse CDTand they had their answers. But I use Linux, why it is NOT working here?
我发现一些相关的问题MinGW的和一个多,到Eclipse CDT的,他们有自己的答案。但是我使用 Linux,为什么它在这里不起作用?
采纳答案by aldo.roman.nurena
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
或者
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
回答by Mike Seymour
std::stoi
is a function at namespace scope, taking a string as its argument:
std::stoi
是命名空间范围内的函数,以字符串作为参数:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string
, invoked as s.stoi()
(or perhaps std::string::stoi(s)
); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.
从错误消息来看,您似乎希望它是 的成员string
,调用为s.stoi()
(或可能std::string::stoi(s)
);事实并非如此。如果那不是问题,那么请发布有问题的代码,这样我们就不需要猜测它出了什么问题。