在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法_第1頁(yè)
在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法_第2頁(yè)
在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法_第3頁(yè)
在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法_第4頁(yè)
在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法//initializeavariable.

//Initializingisprovidingthetype,nameandvalueofthevaribaleinonego.

//outputtotheconsole:"Myageis28",usingchaining,

cout"Myageis:"ageendl;

}//endthemainfunction

如何在C++中聲明和初始化strings

字符串是單個(gè)字符的集合。

在C++中聲明字符串的工作方式與聲明和初始化ints非常相似,你在上面的章節(jié)中看到了這一點(diǎn)。

C++標(biāo)準(zhǔn)庫(kù)提供了一個(gè)string類。為了使用字符串?dāng)?shù)據(jù)類型,你必須在文件的頂部,在#includeiostream之后,包括string頭部庫(kù)。

在包括該頭文件之后,你還可以添加你之前看到的usingnamespacestd;。

在其他方面,加入這一行后,你在創(chuàng)建字符串變量時(shí)將不必使用std::string,只需使用string。

#includeiostream

#includestring

usingnamespacestd;

intmain(){

//declareastringvariable

stringgreeting;

greeting="Hello";

//the`=`istheassignmentoperator,assigningthevaluetothevariable

或者你可以初始化一個(gè)字符串變量并將其打印到控制臺(tái)。

#includeiostream

#includestring

usingnamespacestd;

intmain(){

//initializeastringvariable

stringgreeting="Hello";

//output"Hello"totheconsole

coutgreetingendl;

如前所述,C++是一種強(qiáng)類型的語(yǔ)言。

如果你試圖給出一個(gè)與數(shù)據(jù)類型不一致的值,你會(huì)得到一個(gè)錯(cuò)誤。

另外,將字符串轉(zhuǎn)換為整數(shù)并不像使用類型轉(zhuǎn)換那樣簡(jiǎn)單,你可以在將doubles轉(zhuǎn)換為ints時(shí)使用。

例如,你不能這樣做。

#includeiostream

#includestring

usingnamespacestd;

intmain(){

stringstr="7";

intnum;

num=(int)str;

編譯后的錯(cuò)誤將是。

hellp.cpp:9:10:error:nomatchingconversionforC-stylecastfromstd::__1::string(aka

basic_stringchar,char_traitschar,allocatorchar)toint

num=(int)str;

^~~~~~~~~

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:875:5:note:candidatefunction

operator__self_view()const_NOEXCEPT{return__self_view(data(),size());}

^

1errorgenerated.

有幾種方法可以將字符串轉(zhuǎn)換為int,你會(huì)在后面的章節(jié)中看到其中兩種方法。

如何使用stoi()函數(shù)將字符串轉(zhuǎn)換為int

將字符串對(duì)象轉(zhuǎn)換為數(shù)字int的一個(gè)有效方法是使用stoi()函數(shù)。

這種方法通常用于較新版本的C++,在C++11中被引入。

它將一個(gè)字符串值作為輸入,并將它的整數(shù)版本作為輸出返回。

#includeiostream

#includestring

usingnamespacestd;

intmain(){

//astringvariablenamedstr

stringstr="7";

//printtotheconsole

cout"Iamastring"strendl;

//convertthestringstrvariabletohaveanintvalue

//placethenewvalueinanewvariablethatholdsintvalues,namednum

intnum=stoi(str);

//printtotheconsole

cout"Iamanint"numendl;

輸出。

Iamastring7

Iamanint7

如何使用stringstream類將一個(gè)字符串轉(zhuǎn)換為一個(gè)int

stringstream類主要用于早期版本的C++。它通過(guò)對(duì)字符串進(jìn)行輸入和輸出來(lái)工作。

要使用它,你首先要在你的程序頂部加入sstream庫(kù),加入一行#includesstream。

然后你添加stringstream,并創(chuàng)建一個(gè)stringstream對(duì)象,該對(duì)象將保存你要轉(zhuǎn)換為int的字符串的值,并在轉(zhuǎn)換為int的過(guò)程中使用。

你使用操作符,從字符串變量中提取字符串。

最后,你使用操作符將新轉(zhuǎn)換的int值輸入到int變量中。

#includeiostream

#includestring

#includesstream//thiswillallowyoutousestringstreaminyourprogram

usingnamespacestd;

intmain(){

//createastringstreamobject,toinput/outputstrings

stringstream

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論