码迷,mamicode.com
首页 > 其他好文 > 详细

string与int之间的转换

时间:2020-04-26 19:04:05      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:bsp   std   ber   src   ret   inf   技术   ext   text   

使用stringstream:(stringstream 可以用于各种数据类型之间的转换)

 1 #include <iostream>
 2 #include <sstream>
 3 using namespace std;
 4 int main()
 5 {
 6     int number;
 7     string text="123";
 8     stringstream ss;
 9     ss<<text;
10     ss>>number;
11     cout<<number;
12     return 0;
13  } 

技术图片

 

int->string

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    int number=123;
    string text;
    stringstream ss;
    ss<<number;
    ss>>text;
    cout<<text;
    return 0;
 } 

运行结果和上面一样

 

string与int之间的转换

标签:bsp   std   ber   src   ret   inf   技术   ext   text   

原文地址:https://www.cnblogs.com/cs0915/p/12781383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!