C++double转化为string
2015-06-18 22:45
330 查看
C++ 将double转化为string
cout<<(b->getDescription())<<endl;cout<<(b->cost())<<endl;
2. #include <sstream>
ostringstream sstr; sstr << b->cost(); cout<<(b->getDescription()+ "$ "+ sstr.str());
3. 有许多这种变换的话
// stringcast.h #ifndef _H_STRING_CAST_ #define _H_STRING_CAST_ #include <string> #include <sstream> template <typename T> std::string string_cast(const T& in) { static std::ostringstream stream; stream.str(""); stream << in; return stream.str(); } template <typename T> T string_cast(const std::string& in) { static T out; std::istringstream s(in); s >> out; return out; } #endif You can then just do: std::string str = string_cast<int>(10); EDIT. If you use boost, you can also use lexical_cast
相关文章推荐
- c及c++中的取模运算
- 水仙花数
- C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
- C语言一些细节
- 冒泡排序与快速排序
- C语言找出字符串中的特定的字符,并输出
- c++ 继承多个类 及虚函数
- 夕甲甲——孔乙己之C++版
- 排序算法——堆排序
- c++字符串的包含问题
- 黑马程序员——C语言关键字、标识符、注释、数据类型、常量
- c++的设计模式之备忘录模式
- C++: std::string 与 Unicode 如何结合?
- LeetCode | Majority Element
- C++ MFC常用函数(转)
- C语言实现for循环输出图案
- C语言指针总结
- C++内存管理
- C/C++ 马踏棋盘实现
- C/C++读取文件名(Windows)