c++中的std::stod, stCPP程序说明std::stod():stof, std::stold

简介: std: :stod() : 它将字符串转换为双精度。

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第5天,点击查看活动详情


std: :stod() : 它将字符串转换为双精度。


语法:

double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
Return Value: 返回double类型的值
参数
str : 要转换的字符串
pos : 存储处理的字符数的整数的地址。此参数也可以是空指针,在这种情况下不使用它。
复制代码


// CPP程序说明std::stod()
#include <string>
#include <iostream>
int main(void)
{
  std::string str = "y=4.4786754x+5.6";
  double y, x, a, b;
  y = 0;
  x = 0;
  // 偏移量将设置为“值”-1的字符长度。
  std::size_t offset = 0;
  a = std::stod(&str[2], &offset);
  b = std::stod(&str[offset + 3]);
  std::cout << b;
  return 0;
}
复制代码


输出:


5.6
复制代码


再比如:


// CPP程序说明std::stod()
#include <iostream>
#include <string>
using namespace std;
int main()
{
  string b = "5";
  double a = stod(b);
  int c = stoi(b);
  cout << b << " " << a << " " << c << endl;
}
复制代码


输出:

5 5 5
复制代码


如果未执行转换,则会引发invalid_argument异常。如果读取的值超出双精度的可表示值范围,则会引发out_of_range异常。无效的 idx 会导致未定义的行为。


标准::STOF : 它将字符串转换为浮点数。

语法:


float stof( const string& str, size_t* pos = 0 );
float stof( const wstring& str, size_t* pos = 0 );
参数
str : 要转换的字符串
pos : 用于存储已处理字符数的整数的地址此参数也可以是空指针,在这种情况下,不使用此参数。
Return value: 返回float类型的值。
复制代码


示例 1:


// CPP程序说明std::stof()
#include <iostream>
#include <string>
int main()
{
  std::string x;
  x = "20";
  float y = std::stof(x) + 2.5;
  std::cout << y;
  return 0;
}
复制代码


输出:


22.5
复制代码


示例 2:


// CPP程序说明std::stof()
#include <iostream>
#include <string>
int main()
{
  std::string str = "5000.5";
  float x = std::stof(str);
  std::cout << x;
  return 0;
}
复制代码


输出:


5000.5
复制代码


如果无法执行转换,则会引发invalid_argument异常。


标准::stold : 它将字符串转换为长双精度。


语法:


long double stold( const string& str, size_t *pos = 0 );
long double stold (const wstring& str, size_t* pos = 0);
参数 : 
str : 要转换的字符串
pos : 存储第一个未转换字符的索引的整数地址。
此参数也可以是空指针,在这种情况下不使用它。
Return value : 它返回longdouble类型的值。
复制代码


示例 1:


// CPP程序说明std::stold()
#include <iostream>
#include <string>
int main()
{
  std::string str = "500087";
  long double x = std::stold(str);
  std::cout << x;
  return 0;
}
复制代码


输出:


500087
复制代码


示例 2:


// CPP程序说明std::stold()
#include <iostream>
#include <string>
int main()
{
  std::string x;
  x = "2075";
  long double y = std::stof(x) + 2.5;
  std::cout << y;
  return 0;
}
复制代码


输出:


2077.5



目录
相关文章
|
20天前
|
存储 网络协议 Ubuntu
【C++网络编程】Socket基础:网络通讯程序入门级教程
【C++网络编程】Socket基础:网络通讯程序入门级教程
40 7
|
2月前
|
存储 缓存 算法
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
120 0
|
2月前
|
Java 中间件 API
【C/C++ 线程 】深入浅出:理解 std::thread 的局限性
【C/C++ 线程 】深入浅出:理解 std::thread 的局限性
49 2
|
29天前
|
C++ 计算机视觉 Windows
【C++】由于找不到xxx.dll,无法继续执行代码,重新安装程序可能会解决此问题。(解决办法)
【C++】由于找不到xxx.dll,无法继续执行代码,重新安装程序可能会解决此问题。(解决办法)
|
1月前
|
C++
C++11 std::lock_guard 互斥锁
C++11 std::lock_guard 互斥锁
10 0
|
3天前
|
运维 Serverless Go
Serverless 应用引擎产品使用之在阿里云函数计算中c++模板,将编译好的C++程序放进去部署如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
9 1
|
8天前
|
安全 编译器 C++
C++从入门到精通:3.2异常处理——掌握C++的异常处理机制,提高程序健壮性
C++从入门到精通:3.2异常处理——掌握C++的异常处理机制,提高程序健壮性
|
8天前
|
存储 IDE 编译器
C++从入门到精通:1.3.1了解IDE与C++程序的编写、编译和运行
C++从入门到精通:1.3.1了解IDE与C++程序的编写、编译和运行
|
8天前
|
存储 程序员 数据库
C++从入门到精通:1.2.2简单程序与接收用户输入
C++从入门到精通:1.2.2简单程序与接收用户输入
|
8天前
|
存储 编译器 C++
C++从入门到精通:1.2.1简单程序编写与基本操作
C++从入门到精通:1.2.1简单程序编写与基本操作