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

下載本文檔

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

文檔簡介

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

//Initializingisprovidingthetype,nameandvalueofthevaribaleinonego.

//outputtotheconsole:"Myageis28",usingchaining,

cout"Myageis:"ageendl;

}//endthemainfunction

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

字符串是單個字符的集合。

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

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

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

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

#includeiostream

#includestring

usingnamespacestd;

intmain(){

//declareastringvariable

stringgreeting;

greeting="Hello";

//the`=`istheassignmentoperator,assigningthevaluetothevariable

或者你可以初始化一個字符串變量并將其打印到控制臺。

#includeiostream

#includestring

usingnamespacestd;

intmain(){

//initializeastringvariable

stringgreeting="Hello";

//output"Hello"totheconsole

coutgreetingendl;

如前所述,C++是一種強類型的語言。

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

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

例如,你不能這樣做。

#includeiostream

#includestring

usingnamespacestd;

intmain(){

stringstr="7";

intnum;

num=(int)str;

編譯后的錯誤將是。

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,你會在后面的章節(jié)中看到其中兩種方法。

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

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

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

它將一個字符串值作為輸入,并將它的整數(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類將一個字符串轉(zhuǎn)換為一個int

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

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

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

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

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

#includeiostream

#includestring

#includesstream//thiswillallowyoutousestringstreaminyourprogram

usingnamespacestd;

intmain(){

//createastringstreamobject,toinput/outputstrings

stringstream

溫馨提示

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

評論

0/150

提交評論