关于boost数字转字符串的问题

leonchenjian 2007-06-22 04:26:23
如何让boost::lexical_cast<string>(12345678) 转换出来的字符串是"12345678"
而不是"12,345,678".
先谢过
...全文
982 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
leonchenjian 2007-06-27
  • 打赏
  • 举报
回复
解决了,谢谢akirya
  • 打赏
  • 举报
回复
int main(int,char*[])
{
stringstream c;
c<<12345678;
string str;
c>>str;
cout<<str<<endl;
return 0;
}
这样的程序直接显示的就是没有逗号的
leonchenjian 2007-06-25
  • 打赏
  • 举报
回复
确切的说,我是想知道,
如何在命令行,或者通过编程设置Linux的locale使得数字的显示不使用千位分隔符
  • 打赏
  • 举报
回复
那你在转换前设置
setlocale(LC_ALL,"chs");
redex 2007-06-25
  • 打赏
  • 举报
回复
export LANG=en_US.UTF-8
试试!
leonchenjian 2007-06-25
  • 打赏
  • 举报
回复
代码比较多了,没办法贴,是libtorrent库
  • 打赏
  • 举报
回复
你把你有问题的代码贴出来
  • 打赏
  • 举报
回复
奇怪啊
int main(int,char*[])
{
string str = boost::lexical_cast<string>(12345678);
cout<<str<<endl;
return 0;
}
我这样的代码输出的就是"12345678";
leonchenjian 2007-06-25
  • 打赏
  • 举报
回复
谢谢楼上的兄弟,不过我现在的情况是,我这边用boost::lexical_cast<string>(12345678)转换得到的字符串是"12,345,678",而我需要的是没有逗号间隔的字符串,这个应该可以通过设置Linux的locale来实现。
已经证实的是,在Windows环境下通过SetLocaleInfo设置LOCALE_SGROUPING是可以达到我所说的要求的,但我不知道在Linux下如何设置,网上也有相关的函数,如setlocale及参数说明,但是找不到可以实现我上面要求的例子,所以只能来CSDN求助了
  • 打赏
  • 举报
回复
怎么会编译不过去么?
需要包含
#include<sstream>
#include<string>
#include<iostream>
boost就是使用stringstream转换的.
redex 2007-06-25
  • 打赏
  • 举报
回复
string str = boost::lexical_cast<string>(12345678);
cout << "str is: " << str << endl;

输出: str is: 12345678
环境: redhat el4.4
redex 2007-06-25
  • 打赏
  • 举报
回复
楼上的, 编译不过去, 提示:
error: aggregate `std::stringstream cff' has incomplete type and cannot be defined
dai_weitao 2007-06-24
  • 打赏
  • 举报
回复
显示问题吧
astrophor 2007-06-24
  • 打赏
  • 举报
回复
没遇到过,应该不是lexical_cast的问题,我的就是12345678
为了方便大家使用MinGW(GCC)+_boost.python,特意只做了三个dll,可以很方便地将c++代码为python模块. libboost_python-mgw45-1_49.dll libboost_python-mgw45-d-1_49.dll python27.dll 这三个文件我已放在资源里面,大家可以下载. 下面说说使用方法: 第一步:编写一个hello_ext.cpp的c++源文件 #include <boost/python.hpp> // 第一行必须是#include <boost/python.hpp> // 否则会留下一点小问题 #include // 输出字符串 char const* greet() { return "hello, world"; } // 实现两个数字相加 int add(int x, int y) { return x + y; } // 打印vector的函数 void vprint() { std::vectormyvector; for (int i = 1; i <= 5; i++) { myvector.push_back(i); } std::vector::iterator it; std::cout << "myvector contains:"; for (it = myvector.begin(); it < myvector.end(); it++) { std::cout << " " << *it; } std::cout << std::endl; } // 定义python模块的接口文件 BOOST_PYTHON_MODULE(hello_ext) { // hello_ext为导出python模块的名字 using namespace boost::python; def("greet", greet); // 导出函数greet def("add", add); // 导出函数add def("vprint", vprint); // 导出函数vprint } 将上面的文件一定要保存为utf-8的格式(使用记事本在保存的时候就可以选择),不推荐Ansi格式! 然后就可以使用下面的命令编译python模块了: g++ -o hello_ext.pyd hello_ext.cpp -shared libboost_python-mgw45-1_49.dll python27.dll 也可以使用如下的命令,编译debug版本 g++ -o hello_ext.pyd hello_ext.cpp -shared libboost_python-mgw45-d-1_49.dll python27.dll 运行上面的命令之前,请确保hello_ext.cpp,libboost_python-mgw45-1_49.dll,libboost_python-mgw45-d-1_49.dll和 python27.dll在同一个目录. hello_ext.pyd就是python中能直接使用的动态链接库,windows一般以dll为后缀,而python只承认pyd文件. 下面来测试一下: import hello_ext print hello_ext.greet() print hello_ext.add(1,3) hello_ext.vprint() 输出为: hello, world 4 myvector contains: 1 2 3 4 5 看,成功了! ============================================================================= 使用g++编译常见的问题就是找不到文件<boost/python.hpp>和pyconfig.h等文件. 这些文件其实在boost的目录下面和C:\Python27\include目录中. 为了使用方便,将整个\boost_1_49_0\boost\目录复制到MinGw的include目录下面; 将C:\Python27\include目录下的文件全部复制到MinGw的include目录下面即可. 如果不想复制,也可以给g++设置-L参数 -LC:\boost\include\boost_1_49_0\ 和-LC:\Python27\include, 不过每次都这样,还是麻烦,不如复制一下彻底解决! 在发布hello_ext.pyd的时候,由于是动态链接库,所以不要忘了libboost_python-mgw45-1_49.dll, libboost_python-mgw45-d-1_49.dll和 python27.dll也要一起发布!

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧