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 ++中将整数转换为字符串 。 我们经常遇到需要将输入变量的数据类型从一种类型转换为另一种类型的情况。
The conversion of a variable or value from integer to string can be achieved using either of the following methods:
可以使用以下两种方法之一将变量或值从整数转换为字符串:
C++ stringstream is a stream class. It connects and associates the object (string object) with the stream of the input provided.
C ++ stringstream是一个流类 。 它将对象(字符串对象)与提供的输入流连接并关联。
Syntax:
句法:
stringstream object-name;
<< operator
to insert the integer input value into the stream. 创建stringstream对象之后,我们使用<< operator
将整数输入值插入流中。 >> 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..
24855
Input value after conversion from int to string:
24855
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
在上面的代码片段中,boost :: lexical_cast()方法包含
Output:
输出:
String representation of the input integer value:
101
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:
125
String representation of the input integer value:
125
Either of the following ways can be used to convert a value from string to integer in C++:
在C ++中,可以使用以下两种方法之一将值从字符串转换为整数:
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:
125
Integer representation of the input string value:
125
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
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:
2578
Enter the string2:
257
Integer representation of String input string1:
2578
Integer representation of String input string2:
257
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 ++字符串。
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
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类型,您需要在程序中添加处理这些情况的方法。
翻译自: https://www.journaldev.com/37093/convert-integer-to-string-c-plus-plus