欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++ 整型與字符串的互轉(zhuǎn)方式

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

flyfish

字符串轉(zhuǎn)整型

C的方法 cstr是char*或者const char*類型的字符串

int num = atoi(str);
int num = strtol(cstr, NULL, 10);

//10 表示進(jìn)制

C++11的方法

void test1()
{  
std::string str1 = "1";

std::string str2 = "1.5";

std::string str3 = "1 with words";



int myint1 = std::stoi(str1);

int myint2 = std::stoi(str2);

int myint3 = std::stoi(str3);


std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';

std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';

std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
}

結(jié)果輸出

std::stoi(“1”) is 1 
std::stoi(“1.5”) is 1 
std::stoi(“1 with words”) is 1

//源碼參考cplusplus.com

void test2()
{

 std::string str_dec = "2001, A Space Odyssey";
 std::string str_hex = "40c3";
 std::string str_bin = "-10010110001";
 std::string str_auto = "0x7f";

 std::string::size_type sz;  // alias of size_t

 int i_dec = std::stoi (str_dec,&sz);
 int i_hex = std::stoi (str_hex,nullptr,16);
 int i_bin = std::stoi (str_bin,nullptr,2);
 int i_auto = std::stoi (str_auto,nullptr,0);

 std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
 std::cout << str_hex << ": " << i_hex << '\n';
 std::cout << str_bin << ": " << i_bin << '\n';
 std::cout << str_auto << ": " << i_auto << '\n';

 return 0;
}

輸出

2001, A Space Odyssey: 2001 and [, A Space Odyssey] 
40c3: 16579 
-10010110001: -1201 
0x7f: 127

其他類型 類似

無(wú)符號(hào)整型

stoul

浮點(diǎn)型

stof

數(shù)值轉(zhuǎn)字符串

std::string s;
s = std::to_string(1) + ” is int, “;

其他數(shù)值類型 類似

s = std::to_string(3.14f) + ” is float.”;

以上這篇C++ 整型與字符串的互轉(zhuǎn)方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

上一篇:C語(yǔ)言實(shí)現(xiàn)個(gè)人通訊錄管理系統(tǒng)

欄    目:C語(yǔ)言

下一篇:C++實(shí)現(xiàn)簡(jiǎn)單通訊錄

本文標(biāo)題:C++ 整型與字符串的互轉(zhuǎn)方式

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/72.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有