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

2、

1.数值类型转换为string

1.1使用函数模板+ostringstream

使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成string。

1
2
3
4
5
6
7
8
9
10
11
12
//ostringstream对象用来进行格式化的输出,常用于将各种类型转换为string类型
//ostringstream只支持<<操作符
template<typename T> string toString(const T& t){
    ostringstream oss;  //创建一个格式化输出流
    oss<<t;             //把值传递如流中
    return oss.str();  
}
 
cout<<toString(14.2)<<endl;  //实型->string:输出14.2
cout<<toString(12301)<<endl; //整型->string:输出12301
cout<<toString(123456789785)<<endl;//长整型->string:输出123456789785
cout<<toString(true)<<endl;  //布尔型->string:输出1

  

1.2使用标准库函数std::to_string()

std命令空间下有一个C++标准库函数std::to_string(),可用于将数值类型转换为string。使用时需要include头文件<string>

函数原型申明如下:

1
2
3
4
5
6
7
8
9
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

  

2.string转换为数值类型

2.1使用函数模板+ istringstream

stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> 
#include <sstream>    //使用stringstream需要引入这个头文件 
using namespace std; 
 
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性) 
template <class Type> 
Type stringToNum(const string& str){ 
    istringstream iss(str); 
    Type num; 
    iss >> num; 
    return num;     
 
int main(int argc, char* argv[])  { 
    string str("00801"); 
    cout << stringToNum<int>(str) << endl; 
 
    system("pause"); 
    return 0; 

  

2.2使用C标准库函数

具体做法是先将string转换为char*字符串,再通过相应的类型转换函数转换为想要的数值类型。需要包含标准库函数<stdlib.h>。 
(1)string转换为int32_t

1
2
3
4
5
string love="77";
int ilove=atoi(love.c_str());
 
//或者16位平台转换为long int
int ilove=strtol(love.c_str(),NULL,10);

  (2)string转换为uint32_t

1
2
3
4
5
6
7
8
9
//str:待转换字符串
//endptr:指向str中数字后第一个非数字字符
//base:转换基数(进制),范围从2至36
unsigned long int strtoul (const char* str, char** endptr, int base);
 
#示例
string love="77";
unsigned long ul;
ul = strtoul(love.c_str(), NULL, 10);

  (3)string转换为uint64_t

1
2
string love="77";
long long llLove=atoll(love.c_str());

  (4)string转换为uint64_t

1
2
3
4
5
6
unsigned long long int strtoull (const char* str, char** endptr, int base);
 
#示例
string love="77";
unsigned long long ull;
ull = strtoull (love.c_str(), NULL, 0);

  (5)string转换为float或double

1
2
3
string love="77.77";
float fLove=atof(love.c_str());
double dLove=atof(love.c_str());

  (6)string转换为long double

1
long double strtold (const char* str, char** endptr);

  

2.3使用C++标准库函数

使用C++11引入的C++库函数将string转换为数值类型,相应的库函数申明于头文件<string>中。

名称 原型 说明
stoi int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer (function template )
stol long stol (const string& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long int (function template)
stoul unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned integer (function template)
stoll long long stoll (const string& str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long long (function template)
stoull unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned long long (function template)
stof float stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
Convert string to float (function template )
stod double stod (const string& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
Convert string to double (function template )
stold long double stold (const string& str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
Convert string to long double (function template)

形参说明: 
str:重载了string和wstring版本,表示被转换的字符串。

idx:表示一个size_t*的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符的下标。一般情况下,因为它是直接char型指针把最后非数值字符的地址值和起始地址值相减,所以也表示成功转换的字符数量,如”10”转成功为数值10时,*idx的值为2。

base:表示转换基准,默认是10进制。


参考文献

[1]C++ Reference 
[2]strtoul.C++ Reference 
[2]strtoull.C++ Reference 
[3]strtold.C++ Reference

3、

4、

5、

C++.【转】C++数值类型与string的相互转换的更多相关文章

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

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

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

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

  3. 数值类型与std::string的相互转换

    1.使用std::stringstream: //将in_value值转换成out_type类型 template<class out_type, class in_value> out_ ...

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

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

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

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

  6. 基于Predictive Parsing的ABNF语法分析器(十)——AbnfParser文法解析器之数值类型(num-val)

    ANBF语法中的数值类型有3种:二进制.十进制和十六进制,可以是一个以点号分隔的数列,也可以是一个数值的范围.例如,%d11.22.33.44.55表示五个有次序的十进制数字“11.22.33.44. ...

  7. Java 数值类型以及计算

    前段时候写了一个对外提供的接口,其中有一个数值校验的计算.在测试的过程中发现5.6-1.6 != 4,在反复的测试过程中发现double类型的数值为有精度丢失的现象,看来还是基础知识不牢固,所以就在网 ...

  8. POI使用:用poi接口不区分xls/xlsx格式解析Excel文档(41种日期格式解析方法,5种公式结果类型解析方法,3种常用数值类型精度控制办法)

    一.使用poi解析excel文档 注:全部采用poi接口进行解析,不需要区分xls.xlsx格式,不需要判断文档类型. poi中的日期格式判断仅支持欧美日期习惯,对国内的日期格式并不支持判断,怎么办? ...

  9. sqoop mysql导入hive 数值类型变成null的问题分析

    问题描述:mysql通过sqoop导入到hive表中,发现有个别数据类型为int或tinyint的列导入后数据为null.设置各种行分隔符,列分隔符都没有效果. 问题分析:hive中单独将有问题的那几 ...

随机推荐

  1. 怎样从外网访问内网微服务Microservices?

    本地部署了一个微服务,只能在局域网内访问,怎样从外网也能访问到本地的微服务呢?本文将介绍具体的实现步骤. 准备工作 部署并启动微服务程序 默认部署的微服务端口是8088. 实现步骤 下载并解压hole ...

  2. Linux环境nginx的安装

    安装Nginx前需要编译环境和库文件支持: 1.安装make: yum -y install openssl openssl-devel yum -y install gcc automake aut ...

  3. Linux进程内存分析和内存泄漏定位

    在Linux产品开发过程中,通常需要注意系统内存使用量,和评估单一进程的内存使用情况,便于我们选取合适的机器配置,来部署我们的产品. Linux本身提供了一些工具方便我们达成这些需求,查看进程实时资源 ...

  4. http状态码204/206/200/302/303/307

    HTTP的状态码有很多种,主要有1xx(临时响应).2xx(成功).3xx(已重定向).4xx(请求错误)以及5xx(服务器错误)五个大类,每个大类还对应一些具体的分类.平时我们接触比较多的是200. ...

  5. 配置使用 git 秘钥连接 GitHub

    配置使用 git 秘钥连接 GitHub 在Linux下部署Git环境 1.安装Git. 使用命令安装 git . sudo apt-get install git 2.创建一个 Github 账号 ...

  6. bzoj 4585 烟火表演 - 动态规划 - 可并堆

    题目传送门 传送门I 传送门II 题目大意 给定一棵带边权有根树,修改一条边的边权的代价是修改前和修改后的值的绝对值之差.不能将一条边的边权改为负数.问使得根节点到所有叶节点的距离相等的最小代价. 当 ...

  7. 【python007 -分支和循环】

    一.打飞机游戏的实现逻辑: 加载背景音乐 播放背景音乐(设置单曲循环) 我方飞机诞生 interval = 0  #小飞机没诞生一个就会移动一个位置,那这样的话,会在屏幕出现密密麻麻的飞机,所以要加一 ...

  8. Maven本地仓库引入自定义/第三方的jar

    在cmd下输入如下: mvn install:install-file -Dfile=D:\ojdbc7.jar -DgroupId=com.tech4j.driver -DartifactId=or ...

  9. 旅行商问题【山财新生赛E】

    链接:https://ac.nowcoder.com/acm/contest/547/E 来源:牛客网 题目描述 旅行商来到了一个新的国家,这个国家有N个城市,他们直接由N-1条道路相连接,每条道路的 ...

  10. kubeadm 生成的token过期后,集群增加节点

    通过kubeadm初始化后,都会提供node加入的token: You should now deploy a pod network to the cluster. Run "kubect ...