博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在C ++中将String转换为Integer并将Integer转换为String
阅读量:2534 次
发布时间:2019-05-11

本文共 9080 字,大约阅读时间需要 30 分钟。

In this article, we will understand how to convert an integer to a string in C++. We often come across situations wherein we need to convert the data type of the input variable from one type to another.

在本文中,我们将了解如何在C ++中将整数转换为字符串 。 我们经常遇到需要将输入变量的数据类型从一种类型转换为另一种类型的情况。

在C ++中将整数转换为字符串 (Convert an Integer to String in C++)

The conversion of a variable or value from integer to string can be achieved using either of the following methods:

可以使用以下两种方法之一将变量或值从整数转换为字符串:

  • C++ stringstream class

    C ++ stringstream类
  • C++ boost.lexical_cast() method

    C ++ boost.lexical_cast()方法
  • C++ to_string() method

    C ++ to_string()方法


1. stringstream类 (1. The stringstream class)

C++ is a stream class. It connects and associates the object (string object) with the stream of the input provided.

C ++ 是一个流类 。 它将对象(字符串对象)与提供的输入流连接并关联。

Syntax:

句法:

stringstream object-name;
  • After creating a stringstream object, we use the << operator to insert the integer input value into the stream.

    创建stringstream对象之后,我们使用<< operator将整数输入值插入流中。
  • Further, a string type variable has to be created to store the string value of the entered integer value.

    此外,必须创建一个字符串类型变量来存储输入的整数值的字符串值。
  • Finally, the >> operator is used to read the value from the stringstream object.

    最后, >> operator用于从stringstream对象读取值。

Example:

例:

#include 
#include
using namespace std; int main() { int int_data; cout<<"Input an integer value..\n"; cin>>int_data; stringstream obj; obj<
>string_val; cout<<"Input value after conversion from int to string:\n"<

Output:

输出:

Input an integer value..24855Input value after conversion from int to string:24855


2. boost.lexical_cast()在C ++中将整数转换为字符串 (2. boost.lexical_cast() to Convert integer to string in C++)

C++ has boost/lexical_cast.hpp library containing the boost::lexical_cast() method to convert numbers from integer to string and string to integer.

C ++具有boost / lexical_cast.hpp 库,其中包含boost::lexical_cast()方法,可将数字从整数转换为字符串,并将字符串转换为整数。

In order to use the boost::lexical_cast() method, we will have to include the boost/lexical_cast.hpp header file in our program using the below line:

为了使用boost :: lexical_cast()方法,我们必须使用以下行在程序中包含boost / lexical_cast.hpp头文件:

#include 

Syntax:

句法:

boost::lexical_cast
(variable);

Example:

例:

#include
#include
#include
using namespace std;int main(){int int_val = 101;string x = boost::lexical_cast
(int_val);cout << "String representation of the input integer value:\n";cout <
<

In the above snippet of code, the boost::lexical_cast() method has included <string> as the data type of conversion and accepted an integer value as input.

在上面的代码片段中,boost :: lexical_cast()方法包含<string>作为转换的数据类型,并接受了整数值作为输入。

Output:

输出:

String representation of the input integer value:101


3. to_string()函数在C ++中将整数转换为字符串 (3. to_string() function to Convert Integer to String in C++)

C++ String’s to_string() method can be used to convert an input value from integer to string type.

C ++ String的to_string()方法可用于将输入值从整数转换为字符串类型。

Syntax:

句法:

to_string(value);

Example:

例:

#include
#include
using namespace std;int main(){int int_val;cout<<"Enter an integer value:\n";cin>>int_val;string x = to_string(int_val);cout << "String representation of the input integer value:\n";cout <
<

Output:

输出:

Enter an integer value:125String representation of the input integer value:125


在C ++中将字符串转换为整数 (Convert String to Integer in C++)

Either of the following ways can be used to convert a value from string to integer in C++:

在C ++中,可以使用以下两种方法之一将值从字符串转换为整数:

  • C++ stringstream class

    C ++ stringstream类
  • sscanf() method

    sscanf()方法
  • stoi() method

    stoi()方法
  • atoi() method

    atoi()方法
  • C++ boost::lexical_cast() method

    C ++ boost :: lexical_cast()方法


1. stringstream类 (1. The stringstream class)

As mentioned earlier, C++ stringstream class associates the object (string object) with the stream of the input provided. It can be used to convert the type of the input stream from integer to string and vice-versa.

如前所述, C ++ stringstream类将对象(字符串对象)与提供的输入流相关联。 它可用于将输入流的类型从整数转换为字符串,反之亦然。

Example:

例:

#include
#include
#include
using namespace std;int main(){int int_val = 0; string str_val;cout << "Enter a string value:\n";cin >> str_val;stringstream obj; obj << str_val;obj >> int_val; cout << "Integer representation of the input string value:\n";cout << int_val;return 0; }

Output:

输出:

Enter a string value:125Integer representation of the input string value:125


2. C ++ sscanf()方法 (2. C++ sscanf() Method)

The sscanf() method can be used to convert string to integer in C++.

sscanf()方法可用于在C ++中将字符串转换为整数。

This method does not accept input from the standard stream object. Rather, it accepts input from a string variable and displays it in the integer format using an appropriate format specifier.

此方法不接受来自标准流对象的输入 。 而是,它接受来自字符串变量的输入,并使用适当的格式说明符以整数格式显示它。

Syntax:

句法:

sscanf(string-variable, "%format-specifier", &int_type-variable);

Example:

例:

#include
int main() { const char *str_inp = "4575"; int int_val; sscanf(str_inp, "%d", &int_val); printf("Integer representation of the input string: %d", int_val); return 0; }

In the above code, sscanf() method accepts the input from the str_inp variable of string type. Further, it uses “%d” as the format specifier to convert the string value to the integer type and stores the result in the int_val variable.

在上面的代码中,sscanf()方法接受字符串类型的str_inp变量的输入。 此外,它使用“%d”作为格式说明符将字符串值转换为整数类型,并将结果存储在int_val变量中。

Output:

输出:

Integer representation of the input string: 4575


3. stoi()方法 (3. stoi() method)

The stoi() function in C++ parses a string input and interprets the value of the string as an integer depending on the “base” value provided.

C ++中的stoi()函数解析字符串输入,并根据所提供的“基本”值将字符串的值解释为整数。

Syntax:

句法:

stoi(string-variable, pointer-val, base)
  • string-variable: It represents the string input variable to be converted to an integer type.

    string-variable :表示要转换为整数类型的字符串输入变量。
  • pointer-val (optional): It is a variable (pointer) to point to the character succeeding the last numeric value of the input string-variable.

    pointer-val (optional) :它是一个变量(指针),指向输入字符串变量的最后一个数字值之后的字符。
  • base (optional): It represents the base value to which the integer needs to be represented. The default value is base 10.

    base (optional) :它表示整数必须表示的基本值。 默认值为10

Example:

例:

#include 
#include
using namespace std; int main() { string str_inp1; string str_inp2; cout<<"Enter the string1:\n"; cin>>str_inp1; cout<<"Enter the string2:\n"; cin>>str_inp2; int int_val1 = stoi(str_inp1); int int_val2 = stoi(str_inp2); cout<<"Integer representation of String input string1:\n"; cout<
<

Output:

输出:

Enter the string1:2578Enter the string2:257Integer representation of String input string1:2578Integer representation of String input string2:257


4. atoi()方法 (4. atoi() method)

C++ atoi() method parses the character array/c-string and interprets the string value as a value of integer type. The atoi() method returns a value of type integer.

C ++ atoi()方法解析字符数组/ c-string并将字符串值解释为整数类型的值 。 atoi()方法返回一个integer类型的

Syntax:

句法:

atoi (const char * string-variable);

Example:

例:

#include 
#include
using namespace std; int main() { const char *str_inp1 = "5789"; int int_val1 = atoi(str_inp1); cout<<"Integer representation of String input:\n"; cout<
<

Output:

输出:

Integer representation of String input:5789

Note: The only difference between atoi() and stoi() function is that the atoi() method works on character array and string literals only, while the stoi() function works on character array, string literals as well as c++ strings.

注意: atoi()stoi()函数之间的唯一区别是atoi()方法仅适用于字符数组和字符串文字,而stoi()函数适用于字符数组,字符串文字和c ++字符串。



5. Boost.Lexical_Cast (5. Boost.Lexical_Cast )

Similar to how we converted an int to string using boost lexical cast above, here we will do the opposite and convert a string to int in C++.

与我们使用上面的boost词法转换将int转换为字符串的方式类似,这里我们将做相反的工作,并在C ++中将字符串转换为int。

Example:

例:

#include
#include
#include
using namespace std;int main(){string str_val = "101";int int_val = boost::lexical_cast
(str_val);cout << "Integer representation of the input string value:\n";cout <
<

Output:

输出:

Integer representation of the input string value:101


结论 (Conclusion)

In this article, we have unveiled various techniques to convert an input value from integer type to string and vice-versa. A point to note for all of the examples above is that you need to handle exceptions when the values being converted are not convertible in the specified type.

在本文中,我们揭示了将输入值从整数类型转换为字符串,反之亦然的各种技术。 上面所有示例都需要注意的一点是,当要转换的值在指定类型中不可转换时,您需要处理异常。

For example, “Journaldev” cannot be converted to int type and you need to add methods to handle these cases in your programs.

例如,“ Journaldev”不能转换为int类型,您需要在程序中添加处理这些情况的方法。

翻译自:

转载地址:http://gvqzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>