日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在C ++ STL中使用string :: to_string()将数字转换为字符串

發布時間:2025/3/11 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在C ++ STL中使用string :: to_string()将数字转换为字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

to_string() is a library function of <string> header, it is used to convert numeric value (number) to string.

to_string()是<string>標頭的庫函數,用于將數值(數字)轉換為字符串。

Syntax:

句法:

string to_string(numberic_value);

Here,

這里,

  • string is the return type i.e. function returns an string object that contains the numeric value in string format.

    string是返回類型,即函數返回一個字符串對象,其中包含字符串格式的數字值。

  • numbric_value is the number which can be integer, float, long, double.

    numbric_value是可以為整數,浮點數,長整數,雙精度數的數字。

Example:

例:

#include <iostream> #include <string> using namespace std;int main () {//definition of different types of data typeint intVal =12345;float floatVal = 123.45f;long longVal = 123456789;//converting values to string an printingcout<<"intVal (string format) : "<<to_string (intVal) <<endl;cout<<"floatVal (string format) : "<<to_string (floatVal) <<endl;cout<<"floatVal (string format) : "<<to_string (longVal) <<endl;return 0; }

Output

輸出量

intVal (string format) : 12345floatVal (string format) : 123.449997floatVal (string format) : 123456789 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Expressions results can also be converted to string directly (as the type of expression’s result is numeric)

表達式結果也可以直接轉換為字符串(因為表達式結果的類型是數字)

Consider the example:

考慮示例:

#include <iostream> #include <string> using namespace std;int main () {cout<<to_string (10+20+30+40) <<endl;cout<<to_string (10+20+12.34) <<endl;cout<<to_string (10/20+30*2) <<endl;return 0; }

Output

輸出量

10042.34000060

不使用“使用命名空間std”的函數和對象 (Functions and object without using 'using namespace std')

using namespace std is an statement that tells to the compiler to use namespace named std, if we do not write this statement, then we need to use std:: with all functions, objects.

using namespace std是一條語句,告訴編譯器使用名為std的命名空間,如果我們不編寫此語句,則需要對所有函數,對象使用std :: 。

Consider the example:

考慮示例:

#include <iostream> #include <string>int main () {std::cout<<std::to_string (10+20+30+40) <<std::endl;std::cout<<std::to_string (10+20+12.34) <<std::endl;std::cout<<std::to_string (10/20+30*2) <<std::endl;return 0; }

Output

輸出量

10042.34000060

翻譯自: https://www.includehelp.com/stl/convert-numeric-to-string-using-string-to-string.aspx

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的在C ++ STL中使用string :: to_string()将数字转换为字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。