c++中string轉(zhuǎn)int的方法是什么

c++
小億
140
2024-01-09 05:08:10

在C++中,可以使用std::stoi()函數(shù)將字符串轉(zhuǎn)換為整數(shù)。它的使用方法如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "12345";
    int num = std::stoi(str);
    std::cout << num << std::endl;

    return 0;
}

這將輸出:

12345

std::stoi()函數(shù)還提供了一些額外的參數(shù),比如指定基數(shù)(如二進(jìn)制、八進(jìn)制、十進(jìn)制、十六進(jìn)制等)、處理前導(dǎo)空白字符和檢測(cè)轉(zhuǎn)換成功與否等??梢詤⒖糃++文檔以了解更多關(guān)于std::stoi()函數(shù)的詳細(xì)信息。

0