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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C语言的atoi和C++的to_string

發(fā)布時(shí)間:2023/11/30 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言的atoi和C++的to_string 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
to_stringint to string將其他型轉(zhuǎn)換成字符串型
atoiascii to integer是把字符串轉(zhuǎn)換成整型數(shù)的一個(gè)函數(shù)

to_string

#include <iostream> // std::cout #include <string> // std::string, std::to_stringint main () {std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";std::cout << perfect << '\n';return 0; }

把整型數(shù)據(jù)28(函數(shù)內(nèi)部可進(jìn)行運(yùn)算)和后面的字符串” is a perfect number”進(jìn)行合并

pi is 3.141593 28 is a perfect number

atoi

#include <stdio.h> /* printf, fgets */ #include <stdlib.h> /* atoi */int main () {int i;char buffer[256];printf ("Enter a number: ");fgets (buffer, 256, stdin);i = atoi (buffer);printf ("The value entered is %d. Its double is %d.\n",i,i*2);return 0; } Enter a number: 73 The value entered is 73. Its double is 146.

額外拓展了解即可

1.int/float to string/array:
● ultoa():將無符號(hào)長(zhǎng)整型值轉(zhuǎn)換為字符串。
● gcvt():將浮點(diǎn)型數(shù)轉(zhuǎn)換為字符串,取四舍五入。
● ecvt():將雙精度浮點(diǎn)型值轉(zhuǎn)換為字符串,轉(zhuǎn)換結(jié)果中不包含十進(jìn)制小數(shù)點(diǎn)。
● fcvt():指定位數(shù)為轉(zhuǎn)換精度,其余同ecvt()。

除此外,還可以使用sprintf系列函數(shù)把數(shù)字轉(zhuǎn)換成字符串,其比itoa()系列函數(shù)運(yùn)行速度慢
2. string/array to int/float
C/C++語言提供了幾個(gè)標(biāo)準(zhǔn)庫函數(shù),可以將字符串轉(zhuǎn)換為任意類型(整型、長(zhǎng)整型、浮點(diǎn)型等)。
● atof():將字符串轉(zhuǎn)換為雙精度浮點(diǎn)型值。
● atol():將字符串轉(zhuǎn)換為長(zhǎng)整型值。
● strtod():將字符串轉(zhuǎn)換為雙精度浮點(diǎn)型值,并報(bào)告不能被轉(zhuǎn)換的所有剩余數(shù)字。
● strtol():將字符串轉(zhuǎn)換為長(zhǎng)整值,并報(bào)告不能被轉(zhuǎn)換的所有剩余數(shù)字。
● strtoul():將字符串轉(zhuǎn)換為無符號(hào)長(zhǎng)整型值,并報(bào)告不能被轉(zhuǎn)換的所有剩余數(shù)字。

總結(jié)

以上是生活随笔為你收集整理的C语言的atoi和C++的to_string的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。