c++中atoi()函数的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37229205/
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
use of atoi() function in c++
提问by shubz
when I pass a string
variable in the below code, g++ gives an error:
当我string
在下面的代码中传递一个变量时,g++ 给出了一个错误:
cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string}' to ‘const char*' for argument ‘1' to ‘int atoi(const char*)'
无法将参数 '1' 的 'std::__cxx11::string {aka std::__cxx11::basic_string}' 转换为 'const char*' 到 'int atoi(const char*)'
My code is:
我的代码是:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
string a = "10";
int b = atoi(a);
cout<<b<<"\n";
return 0;
}
But if I change the code to :
但是如果我将代码更改为:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
char a[3] = "10";
int b = atoi(a);
cout<<b<<"\n";
return 0;
}
It works completely fine.
它工作得很好。
Please explain why string
doesn't work. Is there any difference between string a
and char a[]
?
请解释为什么string
不起作用。有什么区别string a
和char a[]
?
回答by Trevor Hickey
atoi
is an older function carried over from C.
atoi
是从 C 继承而来的旧函数。
C did not have std::string
, it relied on null-terminated char arrays instead. std::string
has a c_str()
method that returns a null-terminated char*
pointer to the string data.
C 没有std::string
,它依赖于以空字符结尾的字符数组。 std::string
有一个c_str()
方法返回一个char*
指向字符串数据的空终止指针。
int b = atoi(a.c_str());
In C++11, there is an alternative std::stoi()
function that takes a std::string
as an argument:
在 C++11 中,有一个std::stoi()
将 astd::string
作为参数的替代函数:
#include <iostream>
#include <string>
int main()
{
std::string a = "10";
int b = std::stoi(a);
std::cout << b << "\n";
return 0;
}
回答by Ed Heal
You need to pass a C style string.
您需要传递一个 C 风格的字符串。
I.e use c_str()
即使用 c_str()
Change
改变
int b = atoi(a);
to
到
int b = atoi(a.c_str());
PS:
PS:
This would be better - get the compiler to work out the length:
这会更好 - 让编译器计算出长度:
char a[] = "10";
回答by Remy Lebeau
atoi()
expects a null-terminated char*
as input. A string
cannot be passed as-is where a char*
is expected, thus the compiler error. On the other hand, a char[]
can decay into a char*
, which is why using a char[]
works.
atoi()
期望以空字符结尾char*
作为输入。Astring
不能按原样传递char*
,因此会出现编译器错误。另一方面, achar[]
可以衰减为 a char*
,这就是使用 achar[]
作品的原因。
When using a string
, call its c_str()
method when you need a null-terminated char*
pointer to its character data:
使用 a 时string
,c_str()
当您需要char*
指向其字符数据的空终止指针时调用其方法:
int b = atoi(a.c_str());
回答by C-H-M
There is a difference between them. For each one there are different functions:
它们之间是有区别的。每一种都有不同的功能:
String
细绳
As said before, for string
there is the stoi
function:
如前所述,因为string
有stoi
函数:
string s("20");
cout << stoi(s) * 2; // output: 40
char*
字符*
In the past, atoi
used to handle char*
conversion.
过去,atoi
用于处理char*
转换。
However, now atoi
is replaced by strtol
which gets 3 parameters:
但是, nowatoi
被替换为strtol
which gets 3 个参数:
char*
of the characters to parse tolong
,char**
that returns pointer to after the parsed string,int
for the base the number should be parsed from (2, 10, 16, or whatever).
char*
要解析为的字符long
,char**
返回指向解析后的字符串的指针,int
对于基数,应该从(2、10、16 或其他)解析数字。
char c[]="20";
char* end;
cout << strtol(c, &end, 16); // output: 32
There are whole bunch of functions like strtol
like strtof
, strtod
, or strtoll
which converts to float
, double
, long
, and long long
.
有喜欢的功能一大堆strtol
像strtof
,strtod
或者strtoll
其皈依float
,double
,long
,和long long
。
The main advantages of those new functions are mostly error-handling, and multi-base support.
这些新功能的主要优点主要是错误处理和多基支持。
The main disadvantage is that there isn't a function that converts to int
, only to long
(besides other types).
主要的缺点是没有一个函数可以转换为int
,只能转换为long
(除了其他类型)。
For more details, see https://stackoverflow.com/a/22866001/12893141
回答by stackerjoe
according to the documentation of atoi()
, the function expects a "pointer to the null-terminated byte string to be interpreted"which basically is a C-style string. std::string
is string type in C++ but it have a method c_str()
that can return a C-string which you can pass to atoi()
.
根据 的文档atoi()
,该函数需要一个“指向要解释的空终止字节字符串的指针”,它基本上是一个 C 风格的字符串。std::string
是 C++ 中的字符串类型,但它有一个方法c_str()
可以返回一个 C 字符串,您可以将它传递给atoi()
.
string a = "10";
int b = atoi(a.c_str());
But if you still want to pass std::string
and your compiler supports C++ 11, then you can use stoi()
但是如果你仍然想通过std::string
并且你的编译器支持 C++ 11,那么你可以使用stoi()
string a = "10";
int b = stoi(a);