LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1409|回复: 9

如何将数字写入std::string?

[复制链接]
发表于 2005-4-18 10:59:34 | 显示全部楼层 |阅读模式
<<、+=、append()、及其它赋值方式,都只能写字符串或char  :comp
如果是char *就好办多了,可以sprintf啊 :help
发表于 2005-4-18 12:23:12 | 显示全部楼层
std::strstream
回复 支持 反对

使用道具 举报

发表于 2005-4-18 13:32:26 | 显示全部楼层
  1. #include <boost/lexical_cast.hpp>
  2. #include <string>
  3. #include <iostream>
  4. int main()
  5. {
  6.         using std::string;
  7.         const int d = 123;
  8.         string s = boost::lexical_cast<string>(d);
  9.         std::cout<<s<<std::endl;
  10.         return 0;
  11. }
复制代码


走进Boost
http://www.c-view.org/person/chong/approach2boost.htm
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-18 14:17:21 | 显示全部楼层
锋锋:好
2楼:还是不知道怎么用呢。BTW,怎么查这样的东西的手册呢?man不行;我自己下的资料也是至多只有string类的说明
回复 支持 反对

使用道具 举报

发表于 2005-4-18 14:24:56 | 显示全部楼层
Post by 锋锋
  1. #include <boost/lexical_cast.hpp>
  2. #include <string>
  3. #include <iostream>
  4. int main()
  5. {
  6.         using std::string;
  7.         const int d = 123;
  8.         string s = boost::lexical_cast<string>(d);
  9.         std::cout<<s<<std::endl;
  10.         return 0;
  11. }
复制代码


走进Boost
http://www.c-view.org/person/chong/approach2boost.htm

斑竹啊,有没有深入讲boost的书介绍一本,类示<<stl原码剖析>>
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-19 17:30:13 | 显示全部楼层

给个用法呗?

Post by masterdemon
std::strstream

怎么用?
回复 支持 反对

使用道具 举报

发表于 2005-4-20 16:14:33 | 显示全部楼层
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
    stringstream s;
    s<<1234;
    cout<<s.str()<<endl;
    return 0;
}  

往 stringstream 里写东西和往终端写东西是一样的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-20 16:56:30 | 显示全部楼层

已经知道了,呵

Post by noright
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
    stringstream s;
    s<<1234;
    cout<<s.str()<<endl;
    return 0;
}  

往 stringstream 里写东西和往终端写东西是一样的。

不过这样挺麻烦的,本来往string里写,却还要多用个stringstream转下手
string s(……);stringstream ss;ss << 1;s.append(s.str());
回复 支持 反对

使用道具 举报

发表于 2005-4-21 13:02:12 | 显示全部楼层
Post by ryth
斑竹啊,有没有深入讲boost的书介绍一本,类示<<stl原码剖析>>


呵呵,作为一名“工程人员",能灵活地应用Boost里的例程都很不错了。

想作程序设计高手,cow人   那才去看boost源码,不过看高手的代码,确是一件赏心悦目的事情! :p  特别是可读行高,风格优雅的代码。

具体,也可一google一下,网上也很多师兄们写的boost原码剖析。
回复 支持 反对

使用道具 举报

发表于 2005-4-23 11:46:37 | 显示全部楼层
这里也有一份例子:

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main(int argc, char* argv[])
  5. {
  6.     stringstream ss;
  7.     ss << "hello, " << 100;
  8.     cout << ss.str() << endl;
  9.     return 0;
  10. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表