首页 > 编程语言 > 详细

C++中int型与string型互相转换 - 大气象 - 博客园

时间:2014-01-17 00:14:58      阅读:557      评论:0      收藏:0      [点我收藏+]

C++中int型与string型互相转换 - 大气象 - 博客园

本以为这么多年C#经验,学个C++没多难,现在发现错了。C++真TM难。
今天遇到int转string绊了半天,方法很多,不知道为什么搞那么复杂,
我只挑最简单易懂的,管他效率不效率的。
int转string
int n = 0;
std::stringstream ss;
std::string str;
ss<<n;
ss>>str;
string转int
std::string str = "123";
int n = atoi(str.c_str());
bubuko.com,布布扣
bubuko.com,布布扣
#include "stdafx.h"

#include <string>
#include <sstream>

using namespace std;
void main()
{
    // int 转 string
    stringstream ss;
    int n = 123;
    string str;
    ss<<n;
    ss>>str;
    // string 转 int
    str = "456";
    n = atoi(str.c_str());
}
bubuko.com,布布扣
bubuko.com,布布扣
url: http://greatverve.cn

C++中int型与string型互相转换 - 大气象 - 博客园

原文:http://www.cnblogs.com/lexus/p/3522231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!