在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法
當(dāng)你用C++編碼時,經(jīng)常會有這樣的時候,你會想把一種數(shù)據(jù)類型轉(zhuǎn)換為另一種。
在這篇文章中,你將看到兩種最流行的方法來學(xué)習(xí)如何在C++中把字符串轉(zhuǎn)換為整數(shù)。
讓我們開始吧!
C++中的數(shù)據(jù)類型
C++編程語言有一些內(nèi)置的數(shù)據(jù)類型。
int
,用于整數(shù)(整數(shù))(例如10,150)。double
,用于浮點(diǎn)數(shù)(例如5.0,4.5)。char
,用于單個字符(例如'D','!')。string
, 一系列的字符(例如 "Hello")。bool
,用于布爾值(真或假)。
C++是一種強(qiáng)類型的編程語言,這意味著當(dāng)你創(chuàng)建一個變量時,你必須明確地聲明它將存儲什么類型的值。
如何在C++中聲明和初始化 int s
要在C++中聲明一個int
變量,你需要首先寫出該變量的數(shù)據(jù)類型--本例中是int
。這將讓編譯器知道該變量可以存儲什么類型的值,因此它可以采取什么行動。
接下來,你需要給變量一個名字。
最后,不要忘了用分號來結(jié)束語句。
#include <iostream> int main() { int age; }
然后,你可以給你創(chuàng)建的變量一個值,像這樣。
#include <iostream> int main() { int age; age = 28; }
你可以通過初始化變量和最后打印結(jié)果來組合這些動作,而不是作為單獨(dú)的步驟來做。
// a header file that enables the use of functions for outputing information //e.g. cout or inputing information e.g. cin #include <iostream> // a namespace statement; you won't have to use the std:: prefix using namespace std; int main() { // start of main function of the program int age = 28; // initialize a variable. //Initializing is providing the type,name and value of the varibale in one go. // output to the console: "My age is 28",using chaining, << cout << "My age is: " << age << endl; }// end the main function
如何在C++中聲明和初始化 string s
字符串是單個字符的集合。
在C++中聲明字符串的工作方式與聲明和初始化int
s非常相似,你在上面的章節(jié)中看到了這一點(diǎn)。
C++標(biāo)準(zhǔn)庫提供了一個string
類。為了使用字符串?dāng)?shù)據(jù)類型,你必須在文件的頂部,在#include <iostream>
之后,包括<string>
頭部庫。
在包括該頭文件之后,你還可以添加你之前看到的using namespace std;
。
在其他方面,加入這一行后,你在創(chuàng)建字符串變量時將不必使用std::string
,只需使用string
。
#include <iostream> #include <string> using namespace std; int main() { //declare a string variable string greeting; greeting = "Hello"; //the `=` is the assignment operator,assigning the value to the variable }
或者你可以初始化一個字符串變量并將其打印到控制臺。
#include <iostream> #include <string> using namespace std; int main() { //initialize a string variable string greeting = "Hello"; //output "Hello" to the console cout << greeting << endl; }
如前所述,C++是一種強(qiáng)類型的語言。
如果你試圖給出一個與數(shù)據(jù)類型不一致的值,你會得到一個錯誤。
另外,將字符串轉(zhuǎn)換為整數(shù)并不像使用類型轉(zhuǎn)換那樣簡單,你可以在將double
s轉(zhuǎn)換為int
s時使用。
例如,你不能這樣做。
#include <iostream> #include <string> using namespace std; int main() { string str = "7"; int num; num = (int) str; }
編譯后的錯誤將是。
hellp.cpp:9:10: error: no matching conversion for C-style cast from 'std::__1::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >') to 'int'
num = (int) str;
^~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:875:5: note: candidate function
operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
^
1 error generated.
有幾種方法可以將字符串轉(zhuǎn)換為int,你會在后面的章節(jié)中看到其中兩種方法。
如何使用 stoi() 函數(shù)將字符串轉(zhuǎn)換為int
將字符串對象轉(zhuǎn)換為數(shù)字int的一個有效方法是使用stoi()
函數(shù)。
這種方法通常用于較新版本的C++,在C++11中被引入。
它將一個字符串值作為輸入,并將它的整數(shù)版本作為輸出返回。
#include <iostream> #include <string> using namespace std; int main() { // a string variable named str string str = "7"; //print to the console cout << "I am a string " << str << endl; //convert the string str variable to have an int value //place the new value in a new variable that holds int values, named num int num = stoi(str); //print to the console cout << "I am an int " << num << endl; }
輸出。
I am a string 7
I am an int 7
如何使用stringstream 類將一個字符串轉(zhuǎn)換為一個int
stringstream
類主要用于早期版本的C++。它通過對字符串進(jìn)行輸入和輸出來工作。
要使用它,你首先要在你的程序頂部加入sstream
庫,加入一行#include <sstream>
。
然后你添加stringstream
,并創(chuàng)建一個stringstream
對象,該對象將保存你要轉(zhuǎn)換為int的字符串的值,并在轉(zhuǎn)換為int的過程中使用。
你使用<<
操作符,從字符串變量中提取字符串。
最后,你使用>>
操作符將新轉(zhuǎn)換的int值輸入到int變量中。
#include <iostream> #include <string> #include <sstream> // this will allow you to use stringstream in your program using namespace std; int main() { //create a stringstream object, to input/output strings stringstream ss; // a variable named str, that is of string data type string str = "7"; // a variable named num, that is of int data type int num; //extract the string from the str variable (input the string in the stream) ss << str; // place the converted value to the int variable ss >> num; //print to the consloe cout << num << endl; // prints the intiger value 7 }
總結(jié)
這就是你的成果!你已經(jīng)看到了在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法。
到此這篇關(guān)于在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法的文章就介紹到這了,更多相關(guān)C++ 字符串轉(zhuǎn)換為整數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)utf8字符串和gbk字符串互轉(zhuǎn)
- C++實(shí)現(xiàn)判斷一個字符串是否為UTF8或GBK格式的方法
- C/C++實(shí)現(xiàn)數(shù)字與字符串互相轉(zhuǎn)換的多種方法
- C++中jsoncpp庫和nlohmann-json庫實(shí)現(xiàn)JSON與字符串類型轉(zhuǎn)換
- C++實(shí)現(xiàn)將長整型數(shù)轉(zhuǎn)換為字符串的示例代碼
- C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換成int整形值的示例
- c++中數(shù)字與字符串之間的轉(zhuǎn)換方法(推薦)
- C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制整數(shù)的方法
- C++中utf8字符串和gbk字符串的轉(zhuǎn)換方法
相關(guān)文章
詳解c/c++賦值函數(shù)(重載=號運(yùn)算符)
大家都知道c++里的各種運(yùn)算符都是用函數(shù)實(shí)現(xiàn)的,比如=就等號函數(shù),所以當(dāng)用=給一個對象賦值的時候,實(shí)際調(diào)用的是=號所對應(yīng)的=號函數(shù)。下面通過本文給大家介紹c/c++賦值函數(shù)(重載=號運(yùn)算符),感興趣的朋友一起看看吧2018-08-08??C++11系列學(xué)習(xí)之Lambda表達(dá)式
這篇文章主要介紹了??C++11系列學(xué)習(xí)之Lambda表達(dá)式,C++11終于也引入了lambda表達(dá)式,lambda最早來源于函數(shù)式編程,現(xiàn)代語言慢慢都引入了這個語法,下文關(guān)于??C++11Lambda表達(dá)式相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-04-04C++ read函數(shù)讀入int整形數(shù)據(jù)
這篇文章主要介紹了C++ read函數(shù)讀入int整形數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-07-07基于C++實(shí)現(xiàn)日期計(jì)算器的詳細(xì)教程
在現(xiàn)代社會中,計(jì)算器已經(jīng)進(jìn)入了每一個家庭,人們在生活和學(xué)習(xí)中經(jīng)常需要使用到計(jì)算器,下面這篇文章主要給大家介紹了關(guān)于基于C++實(shí)現(xiàn)日期計(jì)算器的相關(guān)資料,需要的朋友可以參考下2022-06-06C語言實(shí)現(xiàn)線性動態(tài)(單向)鏈表的示例代碼
本文主要介紹了C語言實(shí)現(xiàn)線性動態(tài)(單向)鏈表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C++異步操作future和aysnc與function和bind
這篇文章主要介紹了C++異步操作future和aysnc與function和bind,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09