C++实现字符串和整数的相互转换

2023-01-03 10:08:22

目录字符串转换整数方法1方法2(推荐)整数转换字符串字符串转换整数方法1#includeiostream#includetypeinfousingnamespacestd;intmain()...

目录
字符串转换整数
方法1
方法2(推荐)
整数转换字符串

字符串转换整数

方法1

#include <IOStream>
#include <typeinfo>

using namespace std;

int main() {
string s = "Kinght_123";
cout << typeid(s).name() << '\n';
cout << typeid(atoi(s.c_str())).name();

return 0;
}

输出:

C++实现字符串和整数的相互转换

方法2(推荐)

首先需要引入头文件#include <string>

#include <iostream>
#include <typeinfo>
#include <string>

using namespace std;

int main() {
string s = "Kinght_123";
cout << typeid(s).name() << '\n';
cout << typeid(stoi(s)).name();

return 0;
}

输出:

C++实现字符串和整数的相互转换

整数转换字符串

需要引入头文件#include <string>

#include <iostream>
#include <typeinfo>
#include <string>

using namespace std;

int main() {
int s = 666www.cppcns.com;
cout << typeid(s).name() << '\n';
cout << typeid(to_string(s)).name();

return 0;
}

输出:

C++实现字符串和整数的相互转换

到此这篇关于C++实现字符串和整数的相互转换的文章就介绍到这了,更多相关C++字符串转换整数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!