將1開氏度轉換成°C,在C ++中將String轉換為Integer并將Integer轉換為String

 2023-11-19 阅读 28 评论 0

摘要: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 ++

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++ stringstream is a stream class. It connects and associates the object (string object) with the stream of the input provided.

將1開氏度轉換成°C、 C ++ stringstream是一個流類 。 它將對象(字符串對象)與提供的輸入流連接并關聯。

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 <iostream>  
#include<sstream>  
using namespace std;  
int main() {  int int_data;  cout<<"Input an integer value..\n";  cin>>int_data;  stringstream obj;  obj<<int_data;  string string_val;  obj>>string_val;  cout<<"Input value after conversion from int to string:\n"<<string_val;   
}  

Output:

AD轉換的C? 輸出:


Input an integer value..
24855
Input 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 <boost/lexical_cast.hpp>

Syntax:

F轉換C。 句法:


boost::lexical_cast<data-type>(variable);

Example:

例:


#include<iostream>
#include <boost/lexical_cast.hpp>
#include <string>
using namespace std;
int main()
{
int int_val = 101;
string x = boost::lexical_cast<string>(int_val);
cout << "String representation of the input integer value:\n";
cout <<x;
cout<<endl;
return 0;
}

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:

matlab轉換C語言, 輸出:


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:

excel中將數字作為文本輸入, 例:


#include<iostream>#include <string>
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 <<x;
cout<<endl;
return 0;
}

Output:

輸出:


Enter an integer value:
125
String 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<iostream>
#include<sstream>
#include <string>
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


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

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

c++中int轉string。 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:

c++將int轉換成string, 例:


#include<stdio.h> 
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.

integer怎么轉換成string、 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 <iostream> 
#include <string> 
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<<int_val1<<endl;cout<<"Integer representation of String input string2:\n";cout<<int_val2<<endl;return 0; 
} 

Output:

integer轉換成string, 輸出:


Enter the string1:
2578
Enter the string2:
257
Integer representation of String input string1:
2578
Integer 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:

c++如何將int類型轉換為string類型? 例:


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

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++.

c++中string轉int的方法, 與我們使用上面的boost詞法轉換將int轉換為字符串的方式類似,這里我們將做相反的工作,并在C ++中將字符串轉換為int。

Example:

例:


#include<iostream>
#include <boost/lexical_cast.hpp>
#include <string>
using namespace std;
int main()
{
string str_val = "101";
int int_val = boost::lexical_cast<int>(str_val);
cout << "Integer representation of the input string value:\n";
cout <<int_val;
cout<<endl;
return 0;
}

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類型,您需要在程序中添加處理這些情況的方法。

翻譯自: https://www.journaldev.com/37093/convert-integer-to-string-c-plus-plus

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/182912.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息