当前位置:网站首页>数值类型和字符串之间的转换
数值类型和字符串之间的转换
2022-06-22 14:47:00 【喜欢打篮球的普通人】
1.数值转换为字符串
使用 to_string() 方法可以非常方便地将各种数值类型转换为字符串类型,这是一个重载函数,函数声明位于头文件 中,
- 函数原型如下:
// 头文件 <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);
- 关于函数的使用是非常简单的,示例代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pi = "pi is " + to_string(3.1415926);
string love = "love is " + to_string(5.20 + 13.14);
cout << pi << endl;
cout << love << endl;
return 0;
}
- 测试:

2.字符串转换为数值
由于 C++ 中的数值类型包括整形和浮点型,因此针对于不同的类型提供了不同的函数,通过调用这些函数可以将字符串类型转换为对应的数值类型。
// 定义于头文件 <string>
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
long stol( const std::string& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long stoul( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long long stoull( const std::string& str, std::size_t* pos = 0, int base = 10 );
float stof( const std::string& str, std::size_t* pos = 0 );
double stod( const std::string& str, std::size_t* pos = 0 );
long double stold( const std::string& str, std::size_t* pos = 0 );
- str:要转换的字符串
- pos:传出参数,记录从哪个字符开始无法继续进行解析,比如: 123abc, 传出的位置为 3
- base:若 base 为 0 ,则自动检测数值进制:若前缀为 0 ,则为八进制,若前缀为 0x 或 0X,则为十六进制,否则为十进制。
这些函数虽然都有多个参数,但是除去第一个参数外其他都有默认值,一般情况下使用默认值就能满足需求。
- eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "45";
string str2 = "3.14159";
string str3 = "9527 with words";
string str4 = "words and 2";
int myint1 = std::stoi(str1);
float myint2 = std::stof(str2);
int myint3 = std::stoi(str3);
// 错误: 'std::invalid_argument'
// int myint4 = std::stoi(str4);
cout << "std::stoi(\"" << str1 << "\") is " << myint1 << endl;
cout << "std::stof(\"" << str2 << "\") is " << myint2 << endl;
cout << "std::stoi(\"" << str3 << "\") is " << myint3 << endl;
// cout << "std::stoi(\"" << str4 << "\") is " << myint4 << endl;
}
测试:

从上述测试程序可以得出这样的结论,在 C++11 提供的这些转换函数将字符串转换为数值的过程中:如果字符串中所有字符都是数值类型,整个字符串会被转换为对应的数值,并通过返回值返回
如果字符串的前半部分字符是数值类型,后半部不是,那么前半部分会被转换为对应的数值,并通过返回值返回
如果字符第一个字符不是数值类型转换失败
边栏推荐
- 排序之归并排序
- Navicat Premium 连接Oracle 数据库(图文教程)
- DDD understanding of Domain Driven Design
- 大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
- C language learning -17- function is passed in as a parameter
- 米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)
- 向量1(类和对象)
- Exploration and practice of dewu app data simulation platform
- [leetcode] 9. Palindromes
- vector的模拟实现
猜你喜欢

HMS Core新闻行业解决方案:让技术加上人文的温度
New design of databend SQL planner

再次认识 WebAssembly

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强

pymssql模块使用指南

DevSecOps: CI/CD 流水线安全的最佳实践

【LeetCode】9、回文数
The MIHA tour club in June is hot! 500+ posts, more than HC, just this summer (with internal promotion method)

Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
随机推荐
中信建投证券是跟启牛学堂存在什么关系?开证券账户安全吗
排序之归并排序
Cve-2022-0847 (privilege lifting kernel vulnerability)
Application of mongodb in Tencent retail premium code
向量3(静态成员)
String的模拟实现
Rosbag use command
stack和queue的模拟实现
【山大会议】应用设置模块
数字人民币可以买理财产品了!建行APP在试点地区上线服务专区,实测体验如何?
得物App数据模拟平台的探索和实践
Maze problem (BFS record path)
ORB_VI思想框架
Applet development - Custom expiration cache
百行代码实现基于Redis的可靠延迟队列
【华为云至简致远】征文获奖名单出炉!
使用 zipfile、openpyxl、flask 批量导出excel zip
关于 GIN 的路由树
[Newman] postman generates beautiful test reports
坚持更新博客的动力是什么