c++之string与int型的互转方法归纳

昨天写leetcode oj时要用到string类型转成int型,于是百度了一番,现归纳总结如下。(ps:归纳不全面,大家可补全。。。)


#include<iostream>
#include<string>
#include<sstream>
using namespace std;

class Solution {
public:
    string intTOstring1(int num);   //int型转换为string型,采用ostringstream
    string intTOstring2(int num);   //int型转换为string型,采用stringstream
    string intTOstring3(int num);   //int型转换为string型,采用sprintf
    string intTOstring4(int num);   //int型转换为string型,采用itoa
    int stringTOint1(string s); //string型转换为int型,采用stringstream
    int stringTOint2(string s); //string型转换为int型,采用string.c_str()和atoi

};

string Solution::intTOstring1(int num)
{
    ostringstream os;
    os << num;
    string s;
    s = os.str();   //将int型转换为string型
    return s;
}

string Solution::intTOstring2(int num)
{
    stringstream ss;
    ss << num;
    string s;
    ss >> s;
    return s;
}

string Solution::intTOstring3(int num)
{
    char *ch = new char[20];
    sprintf(ch, "%d", num);
    string s = ch;
    delete ch; 
    ch = NULL;
    return s;
}

string Solution::intTOstring4(int num)
{
    char *ch = new char[20];
    itoa(num, ch, 10);
    string s;
    s = ch;
    delete ch;
    ch = NULL;
    return s;
}

int Solution::stringTOint1(string s)
{
    stringstream ss;
    ss << s;
    int num;
    ss >> num;
    return num;
}

int Solution::stringTOint2(string s)
{
    return atoi(s.c_str());
}


void main()
{
    Solution so;
    cout << "***************  int  ->  string  *******************" << endl;
    cout << "采用ostringstream将int型转换为string型:" << so.intTOstring1(123) << endl;
    cout << "采用stringstream将int型转换为string型:" << so.intTOstring2(101) << endl;
    cout << "采用sprintf将int型转换为string型:" << so.intTOstring3(234) << endl;
    cout << "采用itoa将int型转换为string型:" << so.intTOstring3(876) << endl;
    cout << "\n" << "***************  string  ->  int  *****************" << endl;
    cout << "采用stringstream将string型转换为int型:" << so.stringTOint1("65") << endl;
    cout << "采用string.c_str()和atoi()将string型转换为int型:" << so.stringTOint1("73") << endl;

}

结果:
这里写图片描述


补充:

1) c++中有3个输入输出控制类,分别为:istringstream。ostringstream、stringstream类。代码中用到了后2类。其中:
istringstream类执行C++风格的串流的输入操作。
ostringstream类执行C风格的串流的输出操作。
stringstream类同时支持C风格的串流的输入输出操作。
要使用这3类,必须包含头文件#include<sstream>

2) 当要清空ostringstream中数据时clear()函数不起作用,而应该采用ostringstream.str(“”)。证明如下:

void main()
{
    int num1 = 1, num2 = 2;
    ostringstream os;
    string s1, s2;
    cout << "采用os.clear()清空流中数据,结果如下:" << endl;
    os << num1;
    s1 = os.str();
    cout << "s1 = "<< s1 << endl;
    os.clear();
    os << num2;
    s2 = os.str();
    cout << "s2 = " << s2 << endl;
}

这里写图片描述

void main()
{
    int num1 = 1, num2 = 2;
    ostringstream os;
    string s1, s2;
    cout << "采用os.str("")清空流中数据,结果如下:" << endl;
    os << num1;
    s1 = os.str();
    cout << "s1 = "<< s1 << endl;
    os.str("");
    os << num2;
    s2 = os.str();
    cout << "s2 = " << s2 << endl;
}

这里写图片描述
原因是: ostringstream.clear()
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值