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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Kinght_123   2023-02-03 我要评论

字符串转换整数

方法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;
}

输出:

方法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;
}

输出:

整数转换字符串

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

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

using namespace std;

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

	return 0;
}

输出:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们