START:

写题遇见类型转换又草了,只能写篇博来记录下。

cpp

int to string[^n]

1
2
3
4
5
6
7
8
#include<sstream>
#include<string>
···
Stringstream stream;
sring s;
int n;
stream << n;
stream >> s;

警告

复用要调用stream.clear();

string to int[^n]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<sstream>
using namespace std;
int main(){
stringstream ss;
string s="55";
string s2="666";
int x;
ss<<s;
ss>>x; //x为55
cout<<x<<endl;
ss.clear();
ss<<s2;
ss>>x; //x为666
cout<<x<<endl;
return 0;
}

引用

[^n]: C++ int与string的相互转换(含源码实现)
[^n]:c++下string转int

END

相关文章
评论
分享
Please check the post_relate setting in config.yml of hexo-theme-Annie! You should make sure 'postsEnable == true' and the number of site posts greater than 1.
Please check the comment setting in config.yml of hexo-theme-Annie!