如何将c++字符串转换为int类型?(复制)

时间:2022-09-02 09:12:08

Possible Duplicate:
How to parse a string to an int in C++?

可能重复:如何在c++中将字符串解析为int ?

How do you convert a C++ string to an int?

如何将c++字符串转换为int类型?

Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example).

假设您希望字符串中包含实际的数字(例如“1”、“345”、“38944”)。

Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way.

另外,假设您没有boost,而且您确实希望使用c++的方式,而不是使用过时的C方法。

8 个解决方案

#1


73  

#include <sstream>

// st is input string
int result;
stringstream(st) >> result;

#2


33  

Use the C++ streams.

使用c++流。

std::string       plop("123");
std::stringstream str(plop);
int x;

str >> x;

/* Lets not forget to error checking */
if (!str)
{
     // The conversion failed.
     // Need to do something here.
     // Maybe throw an exception
}

PS. This basic principle is how the boost library lexical_cast<> works.

这个基本原则是boost库lexical_cast<>的工作方式。

My favorite method is the boost lexical_cast<>

我最喜欢的方法是boost lexical_cast<>

#include <boost/lexical_cast.hpp>

int x = boost::lexical_cast<int>("123");

It provides a method to convert between a string and number formats and back again. Underneath it uses a string stream so anything that can be marshaled into a stream and then un-marshaled from a stream (Take a look at the >> and << operators).

它提供了一种方法,可以在字符串和数字格式之间进行转换,然后再返回。在它的下面使用一个字符串流,以便可以将任何可以编组到流中的内容,然后从流中取消编组(查看>>和< operator)。

#3


4  

I have used something like the following in C++ code before:

我之前在c++代码中使用过如下内容:

#include <sstream>
int main()
{
    char* str = "1234";
    std::stringstream s_str( str );
    int i;
    s_str >> i;
}

#4


4  

C++ FAQ Lite

c++ FAQ Lite

[39.2] How do I convert a std::string to a number?

[39.2]如何将std::string转换为number?

https://isocpp.org/wiki/faq/misc-technical-issues#convert-string-to-num

https://isocpp.org/wiki/faq/misc-technical-issues convert-string-to-num

#5


2  

Let me add my vote for boost::lexical_cast

让我为boost添加我的投票:lexical_cast

#include <boost/lexical_cast.hpp>

int val = boost::lexical_cast<int>(strval) ;

It throws bad_lexical_cast on error.

它将bad_lexical_cast设置为错误。

#6


0  

Use atoi

使用atoi

#7


0  

Perhaps I am misunderstanding the question, by why exactly would you not want to use atoi? I see no point in reinventing the wheel.

也许我误解了这个问题,为什么你不愿意使用atoi?我认为重新发明*毫无意义。

Am I just missing the point here?

我是不是漏掉了重点?

#8


-6  

in "stdapi.h"

在“stdapi.h”

StrToInt

This function tells you the result, and how many characters participated in the conversion.

这个函数告诉您结果,以及有多少字符参与了转换。

#1


73  

#include <sstream>

// st is input string
int result;
stringstream(st) >> result;

#2


33  

Use the C++ streams.

使用c++流。

std::string       plop("123");
std::stringstream str(plop);
int x;

str >> x;

/* Lets not forget to error checking */
if (!str)
{
     // The conversion failed.
     // Need to do something here.
     // Maybe throw an exception
}

PS. This basic principle is how the boost library lexical_cast<> works.

这个基本原则是boost库lexical_cast<>的工作方式。

My favorite method is the boost lexical_cast<>

我最喜欢的方法是boost lexical_cast<>

#include <boost/lexical_cast.hpp>

int x = boost::lexical_cast<int>("123");

It provides a method to convert between a string and number formats and back again. Underneath it uses a string stream so anything that can be marshaled into a stream and then un-marshaled from a stream (Take a look at the >> and << operators).

它提供了一种方法,可以在字符串和数字格式之间进行转换,然后再返回。在它的下面使用一个字符串流,以便可以将任何可以编组到流中的内容,然后从流中取消编组(查看>>和< operator)。

#3


4  

I have used something like the following in C++ code before:

我之前在c++代码中使用过如下内容:

#include <sstream>
int main()
{
    char* str = "1234";
    std::stringstream s_str( str );
    int i;
    s_str >> i;
}

#4


4  

C++ FAQ Lite

c++ FAQ Lite

[39.2] How do I convert a std::string to a number?

[39.2]如何将std::string转换为number?

https://isocpp.org/wiki/faq/misc-technical-issues#convert-string-to-num

https://isocpp.org/wiki/faq/misc-technical-issues convert-string-to-num

#5


2  

Let me add my vote for boost::lexical_cast

让我为boost添加我的投票:lexical_cast

#include <boost/lexical_cast.hpp>

int val = boost::lexical_cast<int>(strval) ;

It throws bad_lexical_cast on error.

它将bad_lexical_cast设置为错误。

#6


0  

Use atoi

使用atoi

#7


0  

Perhaps I am misunderstanding the question, by why exactly would you not want to use atoi? I see no point in reinventing the wheel.

也许我误解了这个问题,为什么你不愿意使用atoi?我认为重新发明*毫无意义。

Am I just missing the point here?

我是不是漏掉了重点?

#8


-6  

in "stdapi.h"

在“stdapi.h”

StrToInt

This function tells you the result, and how many characters participated in the conversion.

这个函数告诉您结果,以及有多少字符参与了转换。