C++ 数值与 string 的相互转换
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
C++ 数值与 string 的相互转换相关推荐
- QString与std::string的相互转换
QString与std::string的相互转换 原创 2014年07月07日 16:25:53 4011 [cpp] view plain copy //1 QString与int相互转换 QStr ...
- 第三次学JAVA再学不好就吃翔(part57)--StringBuffer和String的相互转换
学习笔记,仅供参考 文章目录 StringBuffer和String的相互转换 String -- >StringBuffer StringBuffer -- >String 举个例子 S ...
- ASCII码与string的相互转换
ASCII码与string的相互转换 思路: 1)ASCII码转string:把字符(串)直接转换为int类型,即可得到ASCII码: 2)string转ASCII码:将数字转换为字符串转出: {// ...
- [Java基础]int和String的相互转换
int和String的相互转换:
- java中的 BigDecimal 和 String 的相互转换
例子1,string 转BigDecimalpublic class Test{ public static void main(String[] arg) { String str1="2 ...
- C++ int与string的相互转换
C++ int与string的相互转换 本博客转载自:https://www.cnblogs.com/smile233/p/8379802.html 1.int转换成string 1.1 to_str ...
- int和String的相互转换
基本类型包装类的最常见操作是:用于基本类型和字符串之间的相互转换 1.int转换为String public static String valueOf(int i):返回int参数的字符串表示形式. ...
- C++ string类型与数值型变量的相互转换
形参说明: str:重载了string和wstring版本,表示被转换的字符串. idx:表示一个size_t的指针类型,默认为空值.不为空时,转换成功时获取第一个非数值字符的下标.一般情况下,因为它 ...
- android byte转字符串,Andriod | Byte和String的相互转换
核心: String和Byte间的相互转换 1.png 几行代码,教你最简单的转换, 然鹅,在实际应用过程中,数据的复杂性不只是我们简单的两句话就可以转换了的.那么就来看一下我昨天遇到的问题 需求: ...
最新文章
- 笑傲江湖,独孤求败-NetScaler MAS应用交付神功详解
- 【安全漏洞】Emissary 的SSRF漏洞(CVE-2021-32639)发现过程
- 数学--数论--Miller_Rabin判断一个大数是不是素数(随机算法)
- 【转】MFC学习总结
- leetcode —— 1319. 连通网络的操作次数
- 使用jQuery获取视口大小
- http://www.codeproject.com/Questions/117324/upload-file-in-c-with-HttpWebRequest
- 洛谷P2257 YY的GCD(莫比乌斯反演)
- 第3章 如何赢得客户
- java JDBC编程
- 解决鼠标滚动时页面上下跳动的问题
- 群晖NAS如何修改默认404页面
- 7-9 部落 (25分)
- 测试qq和微信voip内网穿透
- oracle 权限问题9017,[数据库]oracle学习笔记(一)用户管理_星空网
- NBA常规赛总得分排行榜(数据截止至11年4月14日)
- 2019-06-17问答系统项目落地调研
- 免费在线文件转换器Convertio
- 甘特图看起来很生硬?教你使用智能颜色装饰你的甘特图!(三)
- 笔记:《深入浅出统计学》第六章:排列与组合(Python实现)