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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:40:38  来源:igfitidea点击:

use of atoi() function in c++

c++atoi

提问by shubz

when I pass a stringvariable 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 stringdoesn't work. Is there any difference between string aand char a[]?

请解释为什么string不起作用。有什么区别string achar a[]

回答by Trevor Hickey

atoiis an older function carried over from C.

atoi是从 C 继承而来的旧函数。

C did not have std::string, it relied on null-terminated char arrays instead. std::stringhas 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::stringas 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 stringcannot 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 时stringc_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 stringthere is the stoifunction:

如前所述,因为stringstoi函数:

string s("20");
cout << stoi(s) * 2; // output: 40

char*

字符*

In the past, atoiused to handle char*conversion.

过去,atoi用于处理char*转换。

However, now atoiis replaced by strtolwhich gets 3 parameters:

但是, nowatoi被替换为strtolwhich gets 3 个参数:

  1. char*of the characters to parse to long,
  2. char**that returns pointer to after the parsed string,
  3. intfor the base the number should be parsed from (2, 10, 16, or whatever).
  1. char*要解析为的字符long
  2. char**返回指向解析后的字符串的指针,
  3. int对于基数,应该从(2、10、16 或其他)解析数字。
char c[]="20";
char* end;
cout << strtol(c, &end, 16); // output: 32

There are whole bunch of functions like strtollike strtof, strtod, or strtollwhich converts to float, double, long, and long long.

有喜欢的功能一大堆strtolstrtofstrtod或者strtoll其皈依floatdoublelong,和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

更多详情,请参见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::stringis 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::stringand your compiler supports C++ 11, then you can use stoi()

但是如果你仍然想通过std::string并且你的编译器支持 C++ 11,那么你可以使用stoi()

string a = "10";
int b = stoi(a);