C++ 数值与 string 的相互转换

C++ 数值与 string 的相互转换使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成string。//ostringstream对象用来进行格式化的输出,常用于将各种类型转换为string类型//ostringstream只支持<<操作符template<typenameT>stringtoString(constT&t){ostringstreamoss;//创建一个格式化输出流

大家好,又见面了,我是你们的朋友全栈君。

1.数值转 string

1.1 函数模板 + ostringstream

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

//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()

C++11 开始,std 名字空间下引入标准库函数 std::to_string(),可用于将数值转换为string。使用时需要 include 头文件<string>

函数原型申明如下:

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 类型转换为常用的数值类型。

#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;
    return 0;  
}  

2.2 C 标准库函数

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

string love = "77";
int ilove = atoi(love.c_str());

//或者16位平台转换为long int
int ilove = strtol(love.c_str(),NULL,10);

(2)string 转换为 uint32_t

// 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 转换为 int64_t

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

(4)string 转换为 uint64_t

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

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

(6)string 转换为 long double

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*的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符相对于首字符的偏移下标,也表示成功转换的字符数量,如 “10” 成功转为数值 10 时,*idx的值为 2。

base:表示数值的基数,默认是 10 进制。


参考文献

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146610.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c++char和int转换_int转换为char数组

    c++char和int转换_int转换为char数组int转char类型

    2022年10月21日
    0
  • Linux网络下载管理工具(lftp, ftp, lftpget, wget)「建议收藏」

    Linux网络下载管理工具(lftp, ftp, lftpget, wget)「建议收藏」网络客户端管理工具在Linux中,通常用网络客户端管理工具实现文件的下载与上传,主要有以下几种,分别为lftp工具,ftp工具,lftpget工具,wget工具,在centos7中,要尽量学会lftp,lftpget等工具,下面多这些工具的简单使用逐一介。lftp使用命令manlftp可查看其具体的使用方法,如果lftp工具未安装,使用yuminstalllftp命令进…

    2022年5月29日
    40
  • servlet的基本原理_简述arp的工作原理

    servlet的基本原理_简述arp的工作原理Web技术成为当今主流的互联网Web应用技术之一,而Servlet是JavaWeb技术的核心基础。因而掌握Servlet的工作原理是成为一名合格的JavaWeb技术开发人员的基本要求。本文将带你认识JavaWeb技术是如何基于Servlet工作,以Tomcat为例了解Servlet容器是如何工作的?一个Web工程在Servlet容器中是如何启动的?

    2022年10月4日
    1
  • 硬核!用Mac Mini翻新了一台12年前的电脑

    硬核!用Mac Mini翻新了一台12年前的电脑本文转载自差评君有这么一位差友,不知道从哪儿加到了托尼的微信,都还没打招呼呢,上来就向我展现了他最近的硬核杰作。他将M1款的MacMini塞进了09年款27寸iMac,并把详细过程一股脑发给了我。尽管不清楚这样做有什么意义,但确实是做的很硬核,手法也相当专业,简单来说,他愣是把一台新电脑,塞进了一台老电脑。。。整理了一下他发过来的内容,大致弄清了这事儿的前因后果。把M1款的Macmini,塞进了09年iMac的想法,是他在看到了油管上有一位叫L…

    2022年5月30日
    86
  • pycharm虚拟环境与本地环境区别_python如何激活虚拟环境

    pycharm虚拟环境与本地环境区别_python如何激活虚拟环境    Python的版本众多,在加上适用不同版本的PythonPackage。这导致在同时进行几个项目时,对库的依赖存在很大的问题。这个时候就牵涉到对Python以及依赖库的版本管理,方便进行开发,virtualenv就是用来解决这个问题的。下面介绍使用PyCharm创建VirtualEnvironment的方法。    PyCharm可以使用virtualenv中的功能来创建虚拟环境。Py…

    2022年8月25日
    6
  • MySQL图形工具SQLyog激活成功教程版

    MySQL图形工具SQLyog激活成功教程版最近一直在用MySQL,所以分享一下现在这套开发工具。SQLyog:链接:http://pan.baidu.com/s/1bLq2OA密码:h5bj注册信息用户名:yunjian注册码:81f43d3dd20872b6JavaIDE工具:IDEA设计工具:PowerDesion数据库:MySQL转载于:https://www.cnbl…

    2022年9月23日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号