将int/double 转string类型
来源:互联网 发布:新人淘宝刷単教程 编辑:程序博客网 时间:2024/04/30 07:30
出处:http://www.cnblogs.com/oomusou/archive/2006/10/10/525679.html
std::string为library type,而int、double为built-in type,两者无法互转,这里使用function template的方式将int转std::string,将double转std:string。
1/**//*
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : ArrayToVectorByConstructor.cpp
5Compiler : Visual C++ 8.0
6Description : Demo how to convert any type to string.
7Release : 11/18/2006
8*/
9#include <iostream>
10#include <sstream>
11#include <string>
12
13template <class T>
14std::string ConvertToString(T);
15
16int main() {
17 std::string s;
18
19 // Convert int to std::string
20 int i = 123;
21 s = ConvertToString(i);
22 std::cout << s << std::endl;
23
24 // Convert double to std::string
25 double d = 123.123;
26 s = ConvertToString(d);
27 std::cout << s << std::endl;
28
29 return 0;
30}
31
32template <class T>
33std::string ConvertToString(T value) {
34 std::stringstream ss;
35 ss << value;
36 return ss.str();
37}
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : ArrayToVectorByConstructor.cpp
5Compiler : Visual C++ 8.0
6Description : Demo how to convert any type to string.
7Release : 11/18/2006
8*/
9#include <iostream>
10#include <sstream>
11#include <string>
12
13template <class T>
14std::string ConvertToString(T);
15
16int main() {
17 std::string s;
18
19 // Convert int to std::string
20 int i = 123;
21 s = ConvertToString(i);
22 std::cout << s << std::endl;
23
24 // Convert double to std::string
25 double d = 123.123;
26 s = ConvertToString(d);
27 std::cout << s << std::endl;
28
29 return 0;
30}
31
32template <class T>
33std::string ConvertToString(T value) {
34 std::stringstream ss;
35 ss << value;
36 return ss.str();
37}
- 将int/double 转string类型
- 一个函数将int,float,double等类型转string
- 如何将int,double转std::string? (C/C++) (template)
- c++ 将string类型转换成int、 long、 double类型函数
- 将string转化为int、double
- 类型转换string,char,int,double
- Java中String类型,int类型,double类型相互转换
- JavaScript将String转为double类型
- C++中int、double类型转换为string类型
- C++中将string类型转换为int, float, double类型
- C++中将string类型转换为int, float, double类型
- C++中将string类型转换为int, float, double类型
- C++中将string类型转换为int, float, double类型
- string类型转换为int, float, double类型、随机数生成
- 将String类型转换成int类型
- 将String类型转为int类型
- 将String类型转换为Int类型
- Java 将String转Double、Double类型保留2位小数
- (转)python中执行linux命令
- 1.16 先ほど、ご紹介いただく久保で
- IEnumerable IEnumerator
- 电脑族防癌七要诀
- Struts2中<jsp:forward page="xxx.action"></jsp:forward>失效
- 将int/double 转string类型
- Nginx+Tomcat+pcre 安装过程和出现的问题以及解决方案
- hello内核模块编译的全过程
- java Web
- 如何在 Visual C# .NET 中使用 SqlDataAdapter 对象更新 SQL Server 数据库
- VSS 2005 使用手册——服务器端 客户端设置
- C#对LinkedListNode排序
- Nginx mp4支持模块
- 央行通报最新百元假币特征 称凭眼看手摸可识别