字符串操作是各種算法題中的???,很多數(shù)據(jù)常常以字符串形式給出,其中有的需要自己轉(zhuǎn)化成整數(shù),而一些整型數(shù)據(jù)有時(shí)轉(zhuǎn)換成字符串處理起來(lái)更加方便,比如判斷一個(gè)整數(shù)是否是回文數(shù),所以字符串和整數(shù)的轉(zhuǎn)換是一些問(wèn)題處理的基礎(chǔ)步驟,C++
?在處理這類問(wèn)題時(shí)并不像?Python
?那樣方便,但是也有許多方法能夠?qū)崿F(xiàn)。
一、string 轉(zhuǎn)為int
1、使用 atoi 轉(zhuǎn)換
#include <iostream>
#include <stdlib.h>
int main()
{
std::string str = "668";
std::cout << atoi(str.c_str());
return 0;
}
atoi
?函數(shù)的頭文件是?stdlib.h
,同樣是一個(gè)C語(yǔ)言中的函數(shù)
2、 使用std::stoi
這個(gè)標(biāo)準(zhǔn)庫(kù)是從C++11開(kāi)始才有的
示例代碼如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x;
string str = "-10";
string str1 = "-10a";
string str2 = "a10";
x = stoi(str);
cout << x << endl;
x = stoi(str1); //stoi()函數(shù)遇到字母時(shí)會(huì)自動(dòng)停下
cout << x << endl;
//x = stoi(str2); //stoi()函數(shù)沒(méi)有數(shù)字的話,程序雖然可以編譯,但運(yùn)行時(shí)會(huì)出錯(cuò)
//cout << x << endl;
return 0;
}
3、?通過(guò) istringstream 轉(zhuǎn)換
#include <iostream>
#include <sstream>
int main()
{
std::string str = "668";
int num = 0;
std::istringstream ss(str);
ss >> num;
std::cout << num;
return 0;
}
使用?istringstream
?可以從字符流中讀取整數(shù),與?ostringstream
?是一種相反的操作
4、使用 sscanf
sscanf
?函數(shù)是 C 語(yǔ)言標(biāo)準(zhǔn)庫(kù)中的函數(shù),可以用于從一個(gè)格式化的字符串中提取數(shù)據(jù)。在 C++ 中,可以使用?sscanf
?函數(shù)將字符串轉(zhuǎn)換為整數(shù)。
下面是一個(gè)示例代碼,展示如何使用?sscanf
?將字符串轉(zhuǎn)換為整數(shù):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
int main() {
std::string str = "12345";
int num;
if (sscanf(str.c_str(), "%d", &num) == 1) {
std::cout << "Converted number: " << num << std::endl;
} else {
std::cout << "Failed to convert the string to an integer." << std::endl;
}
return 0;
}
在上面的代碼中,我們將字符串 "12345" 存儲(chǔ)在?str
?變量中。然后,我們使用?sscanf
?函數(shù)將字符串轉(zhuǎn)換為整數(shù),并將結(jié)果存儲(chǔ)在?num
?變量中。sscanf
?函數(shù)的第一個(gè)參數(shù)是要轉(zhuǎn)換的字符串(使用?c_str()
?方法將?std::string
?轉(zhuǎn)換為 C 風(fēng)格的字符串),第二個(gè)參數(shù)是格式化字符串?"%d"
,用于指定要提取的數(shù)據(jù)類型為整數(shù)。如果轉(zhuǎn)換成功,sscanf
?函數(shù)將返回成功轉(zhuǎn)換的變量數(shù)目(在這個(gè)例子中為 1),否則返回值為 0。
以上代碼的輸出將是:
Converted number: 12345
請(qǐng)注意,sscanf
?函數(shù)可以根據(jù)不同的格式化字符串提取不同類型的數(shù)據(jù)。如果你的字符串中包含其他數(shù)據(jù)類型,你可以相應(yīng)地修改格式化字符串和變量的類型。
【注】如果你使用的是VS編譯器,那么會(huì)報(bào)錯(cuò)說(shuō)sscanf不安全,改為sscanf_s即可。
5、使用strtol
使用c標(biāo)準(zhǔn)庫(kù)
#include <stdlib.h>
long int strtol(const char *nptr, char **endptr, int base);
示例代碼:
#include <iostream>
#include <cstdlib>
#include <string>
int main()
{
std::string text{"123"};
errno = 0; // pre set to 0
int number = (int)std::strtol(text.c_str(), nullptr, 10);
if (errno == ERANGE) {
// the number is too big/small
// number = (int)LONG_MAX or (int)LONG_MIN
std::cerr << "Too big or small: " << errno << "\n";
return 1;
} else if (errno) {
// maybe EINVAL, E2BIG or EDOM
// unable to convert to a number
std::cerr << "ERROR: " << errno << "\n";
return 1;
}
// TODO: you need to check whether the long to int overflow too if neccessary
std::cout << number << "\n";
return 0;
}
6、使用at()函數(shù)定位,先轉(zhuǎn)為字符型,然后再轉(zhuǎn)為int類型
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "a1b2c3";
int i = str.at(1) - '0';
cout << i << endl;
return 0;
}
二、 int轉(zhuǎn)為string
1、通過(guò) std::to_string() 函數(shù)
#include <iostream>
#include <string> // 添加這個(gè)頭文件
int main() {
int num = 123;
std::cout << std::to_string(num);
return 0;
}
這種方式在 C++11 中才能使用,編譯時(shí)記得加上?--std=c++11
?的選項(xiàng)
2、通過(guò) ostringstream
#include <iostream>
#include <sstream>
int main()
{
int num = 123;
std::ostringstream ss;
ss << num;
std::cout << ss.str();
return 0;
}
這是一種通過(guò)字符流的方式將整數(shù)轉(zhuǎn)換成字符串,這種方式在C++11之前也可以使用
3、通過(guò) sprintf
#include <stdio.h>
int main()
{
int num = 123;
char buffer[256];
sprintf(buffer, "%d", num);
printf("%s", buffer);
return 0;
}
【注】上述代碼在VS編譯器中運(yùn)行時(shí),會(huì)報(bào)sprintf不安全,換為sprintf_s即可文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-763205.html
這是一種C語(yǔ)言中的轉(zhuǎn)換方式,sprintf
?也可以換成更安全的?snprintf
?函數(shù),如下所示。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-763205.html
#include <iostream>
#include <cstdio>
int main() {
char buffer[100];
int num = 123;
// 使用 snprintf 將整數(shù) num 格式化為字符串并存儲(chǔ)在 buffer 中
snprintf(buffer, sizeof(buffer), "%d", num);
// 使用 std::cout 輸出 buffer 中的內(nèi)容
std::cout << "buffer 中的內(nèi)容是:" << buffer << std::endl;
return 0;
}
到了這里,關(guān)于C++中string類型和int類型之間的相互轉(zhuǎn)換【全網(wǎng)最全】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!