1.使用std::stringstream:

//将in_value值转换成out_type类型
template<class out_type, class in_value>
out_type StringTo(const in_value& t)
{
std::stringstream sstream;
sstream << t; //向流中传值
out_type result; //这里存储转换结果
sstream >> result; //向result中写入值
return result;
}

2.使用std::ostringstream和std::istringstream

//将各种类型转换为string类型
template <typename T>
std::string toString(const T& t)
{
std::ostringstream oss; //创建一个格式化输出流
oss << t; //把值传递到流中
return oss.str();
} //将string类型转换为常用的数值类型
template <class Type>
Type ConvertTo(const std::string& str)
{
std::istringstream iss(str);
Type type;
iss >> type;
return type;
}

3.大小写转换:

//小写
std::string& ToLower(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), tolower);
return str;
} //大写
std::string& ToUpper(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), toupper);
return str;
}

数值类型与std::string的相互转换的更多相关文章

  1. C++11中string与数值类型的转换

    C++中string与数值类型的相互转换记录 string转int.double.long string s = "123.456"; // string -> int co ...

  2. C++标准库里自带的数值类型和字符串互相转换函数

    需要包含头文件 #include <string> 数值类型转成string类型: string to_string(int val); string to_string(unsigned ...

  3. std::string find 的返回值

    std::string  的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始), 如果未查找到,该返回值是一个很大的数据(4294 ...

  4. c++之常见数据类型(int,double,float,long double long long 与std::string之间)相互转换(含MFC的CString、含C++11新特性函数)

    --- 已经通过初步测试---- ------------------ 下面的是传统常见数据类型的转换(非c++11)---------------  std::string 与其他常用类型相互转换, ...

  5. C++.【转】C++数值类型与string的相互转换

    1.C++数值类型与string的相互转换 - JohnGu - 博客园.html(https://www.cnblogs.com/johngu/p/7878029.html) 2. 1.数值类型转换 ...

  6. C++数值类型与string的相互转换

    转自:https://www.cnblogs.com/johngu/p/7878029.html 1.数值类型转换为string 1.1使用函数模板+ostringstream 使用函数模板将基本数据 ...

  7. std::string和int类型的相互转换(C/C++)

    字符串和数值之前转换,是一个经常碰到的类型转换. 之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考: 优缺点:C++的stringstream智 ...

  8. c++11 数值类型和字符串的相互转换

    string和数值类型转换 c++11提供了to_string方法,可以方便的将各种数值类型转换为 字符串类型: std::string to_string(int value); std::stri ...

  9. C++中string转化为常用数值类型

    //模板类 用于将string类型转化为 常用数值类型 template <class Type> Type stringToNum(const string& str) { is ...

随机推荐

  1. 前端逼死强迫症系列之javascript续集

    一.javascript函数 1.普通函数 function func(){ } 2.匿名函数 setInterval(function(){ console.log(123); },5000) 3. ...

  2. phpstorm 2019.1 mac

    链接:https://pan.baidu.com/s/10x0Oa24aOZHJYCYgUGe8yg  密码:muah 安装完成后, sudo vi /etc/hosts 添加以下内容到hosts 0 ...

  3. 6.linux 用户和权限的建立

    一.用户和权限的建立       su  用户名       切换用户,如果是root用户切换其他用户,不需要输入密码.     exit  可以切换回上一个用户       linux 操作系统用户 ...

  4. Oracle虚拟机 与 windows配置

    目录 相关资料 安装虚拟机及相关配置 安装PLSQL Developer Navicat Premium登录数据库 踩坑之路 相关资料 oracle虚拟机配置 提取码:s3sg 安装虚拟机及相关配置 ...

  5. js的一些兼容性易错的的问题

    一.属性相关 我们通常把特征(attribute)和属性(property)统称为属性,但是他们确实是不同的概念,特征(attribute)会表现在HTML文本中,对特征的修改一定会表现在元素的out ...

  6. What is the yield keyword used for in C#?

    What is the yield keyword used for in C#? https://stackoverflow.com/a/39496/3782855 The yield keywor ...

  7. 一个读取C#特性Description方法(zhuan)

    class Program { static void Main(string[] args) { string str= DB.write.ToDescription(); Console.Writ ...

  8. C#-片段-插入片段:Visual C#

    ylbtech-C#-片段-插入片段:Visual C# 1.返回顶部 ·#if #if true #endif ·#region #region MyRegion #endregion · 2.返回 ...

  9. springBoot注解搜集

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  10. linux下程序启动后后台运行实现

    关于linux下的程序运行很简单,将源码编译成二进制(假设为proram)文件后直接在命令行运行即可,root#./program如果需要后台运行,即不占用当前终端,这在嵌入式linux显得十分有必要 ...