类型转化

方法一:

 1 #include<sstream>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int main() {
 6     stringstream ss;
 7     int n;
 8     string s;
 9     cin>>n;
10     ss<<n;
11     ss>>s;
12     cout<<s<<endl;
13     ss.clear();//重置流的状态
14     cin>>s;
15     ss<<s;
16     ss>>n;
17     cout<<n<<endl;
18 }

方法二:

#include<cstdio>
int main() {
    char s[10];
    scanf("%s",s);
    int n;
    sscanf(s,"%d",&n); //把s以%d写入n
    printf("%d\n",n);
    n++;
    sprintf(s,"%d",n); //把n以%s形写入s
    printf("%s",s);
    return 0;
}

 

posted @ 2019-02-14 20:00  Frontierone  阅读(171)  评论(0编辑  收藏  举报