日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++實現字符串和整數的相互轉換_C 語言

作者:Kinght_123 ? 更新時間: 2023-02-07 編程語言

字符串轉換整數

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

輸出:

原文鏈接:https://blog.csdn.net/Kinght_123/article/details/128520784

欄目分類
最近更新