最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年06月10日 09:45:16   作者:迪魯賓  
經(jīng)常會(huì)遇到類(lèi)型轉(zhuǎn)換,本文主要介紹了C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

當(dāng)你用C++編碼時(shí),經(jīng)常會(huì)有這樣的時(shí)候,你會(huì)想把一種數(shù)據(jù)類(lèi)型轉(zhuǎn)換為另一種。

在這篇文章中,你將看到兩種最流行的方法來(lái)學(xué)習(xí)如何在C++中把字符串轉(zhuǎn)換為整數(shù)。

讓我們開(kāi)始吧!

C++中的數(shù)據(jù)類(lèi)型

C++編程語(yǔ)言有一些內(nèi)置的數(shù)據(jù)類(lèi)型。

  • int,用于整數(shù)(整數(shù))(例如10,150)。
  • double,用于浮點(diǎn)數(shù)(例如5.0,4.5)。
  • char,用于單個(gè)字符(例如'D','!')。
  • string, 一系列的字符(例如 "Hello")。
  • bool,用于布爾值(真或假)。

C++是一種強(qiáng)類(lèi)型的編程語(yǔ)言,這意味著當(dāng)你創(chuàng)建一個(gè)變量時(shí),你必須明確地聲明它將存儲(chǔ)什么類(lèi)型的值。

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

要在C++中聲明一個(gè)int 變量,你需要首先寫(xiě)出該變量的數(shù)據(jù)類(lèi)型--本例中是int 。這將讓編譯器知道該變量可以存儲(chǔ)什么類(lèi)型的值,因此它可以采取什么行動(dòng)。

接下來(lái),你需要給變量一個(gè)名字。

最后,不要忘了用分號(hào)來(lái)結(jié)束語(yǔ)句。

#include <iostream>

int main() {
    int age;
}

然后,你可以給你創(chuàng)建的變量一個(gè)值,像這樣。

#include <iostream>

int main() {
    int age;
    age = 28;
}

你可以通過(guò)初始化變量和最后打印結(jié)果來(lái)組合這些動(dòng)作,而不是作為單獨(dú)的步驟來(lái)做。

// 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

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

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

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

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

在其他方面,加入這一行后,你在創(chuàng)建字符串變量時(shí)將不必使用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

}

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

#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)類(lèi)型的語(yǔ)言。

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

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

例如,你不能這樣做。

#include <iostream>
#include <string>
using namespace std;

int main() {
   string str = "7";
   int num;

   num = (int) str;
}

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

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

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

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

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

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

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

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

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

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

最后,你使用>> 操作符將新轉(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ù)的兩種簡(jiǎn)單方法。

到此這篇關(guān)于在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡(jiǎn)單方法的文章就介紹到這了,更多相關(guān)C++ 字符串轉(zhuǎn)換為整數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++入門(mén)之vector的底層實(shí)現(xiàn)詳解

    C++入門(mén)之vector的底層實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了C++入門(mén)之vector的底層實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • C++ 容器適配器priority_queue的使用及實(shí)現(xiàn)代碼

    C++ 容器適配器priority_queue的使用及實(shí)現(xiàn)代碼

    這篇文章主要介紹了C++ 容器適配器priority_queue的使用及實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 從頭學(xué)習(xí)C語(yǔ)言之二維數(shù)組

    從頭學(xué)習(xí)C語(yǔ)言之二維數(shù)組

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言之二維數(shù)組,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • C語(yǔ)言實(shí)現(xiàn)自動(dòng)給QQ好友發(fā)窗口抖動(dòng)

    C語(yǔ)言實(shí)現(xiàn)自動(dòng)給QQ好友發(fā)窗口抖動(dòng)

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)自動(dòng)給QQ好友發(fā)窗口抖動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C++全面覆蓋內(nèi)存管理知識(shí)講解

    C++全面覆蓋內(nèi)存管理知識(shí)講解

    本章主要介紹C語(yǔ)言與C++的內(nèi)存管理,以C++的內(nèi)存分布作為引入,介紹C++不同于C語(yǔ)言的內(nèi)存管理方式(new delete對(duì)比 malloc free),感興趣的朋友來(lái)看看吧
    2022-06-06
  • 深入解析 C++中std::stoul 函數(shù)

    深入解析 C++中std::stoul 函數(shù)

    std::stoul是 C++ 標(biāo)準(zhǔn)庫(kù)中的一個(gè)字符串轉(zhuǎn)換函數(shù),它用于將?std::string?或?std::wstring?轉(zhuǎn)換為?unsigned long?類(lèi)型的整數(shù),下面就來(lái)介紹一下
    2025-04-04
  • c語(yǔ)言實(shí)現(xiàn)足球比賽積分統(tǒng)計(jì)系統(tǒng)

    c語(yǔ)言實(shí)現(xiàn)足球比賽積分統(tǒng)計(jì)系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了c語(yǔ)言實(shí)現(xiàn)足球比賽積分統(tǒng)計(jì)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • c++關(guān)鍵字const的用法詳解

    c++關(guān)鍵字const的用法詳解

    在類(lèi)中,如果你不希望某些數(shù)據(jù)被修改,可以使用const關(guān)鍵字加以限定。const 可以用來(lái)修飾成員變量、成員函數(shù)以及對(duì)象,希望能夠給你帶來(lái)幫助
    2021-09-09
  • C++實(shí)現(xiàn)LeetCode(98.驗(yàn)證二叉搜索樹(shù))

    C++實(shí)現(xiàn)LeetCode(98.驗(yàn)證二叉搜索樹(shù))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(98.驗(yàn)證二叉搜索樹(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法

    用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法

    這篇文章主要介紹了用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法,即pow()函數(shù)和sqrt()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08

最新評(píng)論

沂源县| 焦作市| 晴隆县| 开平市| 白玉县| 昌乐县| 巴彦县| 巴彦县| 乌恰县| 商丘市| 乌拉特后旗| 蒙阴县| 红桥区| 扶沟县| 潜山县| 丹棱县| 虹口区| 肥西县| 南平市| 当阳市| 聂拉木县| 湛江市| 罗源县| 垦利县| 樟树市| 磴口县| 岑溪市| 饶平县| 绥阳县| 汤原县| 泰和县| 隆尧县| 墨竹工卡县| 建德市| 天镇县| 景东| 湟源县| 清原| 自贡市| 阿巴嘎旗| 岳阳县|