天天看點

數字字元_在C++中将字元串轉換為數字

數字字元_在C++中将字元串轉換為數字

有許多情況需要将數字轉換為字元串或将字元串轉換為數字。本文中提到了一些實作此任務的方法。

将字元串轉換為數字

方法1:使用stringstream類或sscanf()

方法2:使用stoi()或atoi()進行字元串轉換

方法3:使用boost lexical_cast

Boost庫提供了一個内置函數lexical_cast("string"),該函數直接将字元串轉換為數字。如果輸入無效,則傳回異常“bad_lexical_cast”。

//C++ code to demonstrate working of lexical_cast() #include #include // for lexical_cast() #include  // for string using namespace std; int main() {    string str = "5";    string str1 = "6.5";      // Initializing f_value with casted float    // f_value is 6.5    float f_value = boost::lexical_cast(str1);      // Initializing i_value with casted int    // i_value is 5    int i_value = boost::lexical_cast(str);      //Displaying casted values    cout << "The float value after casting is : ";    cout << f_value <
           

輸出:

The float value after casting is : 6.5The int value after casting is : 5
           

将數字轉換為字元串

方法1:使用字元串流

在此方法中,字元串流聲明一個流對象,該對象首先将一個數字作為流插入對象,然後使用“str()”跟随數字到字元串的内部轉換。

// C++ code to demonstrate string stream method // to convert number to string. #include #include   // for string streams #include   // for string using namespace std; int main() {     int num = 2016;       // declaring output string stream     ostringstream str1;       // Sending a number as a stream into output     // string     str1 << num;       // the str() coverts number into string     string geek = str1.str();       // Displaying the string     cout << "The newly formed string from number is : ";     cout << geek << endl;       return 0; } 
           

輸出:

The newly formed string from number is : 2016
           

方法2:使用to_string()

該函數接受一個數字(可以是任何資料類型),并以所需的字元串形式傳回該數字。

// C++ code to demonstrate "to_string()" method // to convert number to string. #include #include // for string and to_string() using namespace std; int main() {     // Declaring integer     int i_val = 20;       // Declaring float     float f_val = 30.50;       // Conversion of int into string using     // to_string()     string stri = to_string(i_val);       // Conversion of float into string using     // to_string()     string strf = to_string(f_val);       // Displaying the converted strings     cout << "The integer in string is : ";     cout << stri << endl;     cout << "The float in string is : ";     cout << strf << endl;       return 0;     } 
           

輸出:

The integer in string is : 20The float in string is : 30.500000
           

方法3:使用boost lexical_cast

與字元串轉換類似,“lexical_cast()”函數保持不變,但是這次參數清單修改為“lexical_cast(numeric_var)”。

// C++ code to demonstrate "lexical_cast()" method // to convert number to string. #include  // for lexical_cast() #include  // for string using namespace std; int main() {      // Declaring float    float f_val = 10.5;      // Declaring int    int i_val = 17;         // lexical_cast() converts a float into string    string strf = boost::lexical_cast(f_val);          // lexical_cast() converts a int into string    string stri = boost::lexical_cast(i_val);          // Displaying string converted numbers    cout << "The float value in string is : ";    cout << strf << endl;    cout << "The int value in string is : ";    cout << stri << endl;         return 0;    } 
           

輸出:

The float value in string is : 10.5The int value in string is : 17