C++ string和int相互转换

首先需要C++ 11的支持

打开devC++,点击tools,点击编译环境,然后出现的框第一个勾选,输入-std=c++11即可

然后使用 to_string() 和 atoi() 就可以轻松实现其相互转换

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <sstream>
 5 typedef long long ll;
 6 using namespace std;
 7 int main()
 8 {    
 9     // int -> string
10     int x = 3;
11     string s = to_string(x);
12     cout << s << endl;
13     
14     // string -> int
15     string a = "124";
16     int b = atoi(a.c_str());
17     cout << b;
18     
19 }

猜你喜欢

转载自www.cnblogs.com/wzy-blogs/p/9194764.html