std::stox类型

news2024/10/6 20:33:24

std::stod

函数原型

double stod (const string&  str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);

函数功能
将std::string字符串转化为双精度类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置

函数返回值
如果成功则返回转换的double型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

这里说明一下参数idx的意义和作用:
如果当我们的字符串中只有一个double型数值时我们可以这样进行转化:

#include <iostream>
#include <string>

int main()
{
	std::string numStr = "5646545.32";

	double number1 = std::stod(numStr);
	return 0;
}

但是如果字符串中有两个double型的数值该如何进行转化呢?这个时候我们就可以使用参数idx存储上一个double数值解析完成之后的在字符串中的索引,代码如下:

#include <iostream>
#include <string>

int main()
{
	std::string numStr = "5646545.32 3.1415923";
	size_t idx = 0;

	double number1 = std::stod(numStr, &idx);
	double number2 = std::stod(numStr.substr(idx));

	return 0;
}

std::stof

函数原型

float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);

函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。

函数返回值
如果成功则返回转换的float型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

#include <iostream>
#include <string>

int main()
{
	std::string orbits("686.97 365.24");
	std::string::size_type sz;     // alias of size_t

	float mars = std::stof(orbits, &sz);
	float earth = std::stof(orbits.substr(sz));
	std::cout << "One martian year takes " << (mars / earth) << " Earth years.\n";
	return 0;
}

注意float的有效位为6-7位,如果超过的话,精度会丢失

std::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);

函数功能
将std::string字符串转换为int类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
base  转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的int型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main()
{
	std::string str_dec = "2001, A Space Odyssey";
	std::string str_hex = "40c3";
	std::string str_bin = "-10010110001";
	std::string str_auto = "0x7f";

	std::string::size_type sz;   // alias of size_t

	int i_dec = std::stoi(str_dec, &sz);
	int i_hex = std::stoi(str_hex, nullptr, 16);
	int i_bin = std::stoi(str_bin, nullptr, 2);
	int i_auto = std::stoi(str_auto, nullptr, 0);

	std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
	std::cout << str_hex << ": " << i_hex << '\n';
	std::cout << str_bin << ": " << i_bin << '\n';
	std::cout << str_auto << ": " << i_auto << '\n';

	return 0;
}

在这里插入图片描述

std::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);

函数功能
将std::string字符串转换为long int类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
base  转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的long int型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include <iostream>   // std::cout
#include <string>     // std::string, std::stol

int main()
{
	std::string str_dec = "1987520";
	std::string str_hex = "2f04e009";
	std::string str_bin = "-11101001100100111010";
	std::string str_auto = "0x7fffff";

	std::string::size_type sz;   // alias of size_t

	long li_dec = std::stol(str_dec, &sz);
	long li_hex = std::stol(str_hex, nullptr, 16);
	long li_bin = std::stol(str_bin, nullptr, 2);
	long li_auto = std::stol(str_auto, nullptr, 0);

	std::cout << str_dec << ": " << li_dec << '\n';
	std::cout << str_hex << ": " << li_hex << '\n';
	std::cout << str_bin << ": " << li_bin << '\n';
	std::cout << str_auto << ": " << li_auto << '\n';

	return 0;
}

在这里插入图片描述

std::stold

函数原型

long double stold (const string&  str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);

函数功能
将std::string字符串转换为long double类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置

函数返回值
如果成功则返回转换的long double型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main()
{
	std::string orbits("90613.305 365.24");
	std::string::size_type sz;     // alias of size_t

	long double pluto = std::stod(orbits, &sz);
	long double earth = std::stod(orbits.substr(sz));
	std::cout << "Pluto takes " << (pluto / earth) << " years to complete an orbit.\n";
	return 0;
}

std::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);

函数功能
将std::string字符串转换为long long类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置
base  转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的long long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

#include <iostream>   // std::cout
#include <string>     // std::string, std::stoll

int main()
{
	std::string str = "8246821 0xffff 020";

	std::string::size_type sz = 0;   // alias of size_t

	while (!str.empty()) {
		long long ll = std::stoll(str, &sz, 0);
		std::cout << str.substr(0, sz) << " interpreted as " << ll << '\n';
		str = str.substr(sz);
	}

	return 0;
}

在这里插入图片描述

std::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);

函数功能
将std::string字符串转换为unsigned long类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
base 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的unsigned long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

#include <iostream>   // std::cin, std::cout
#include <string>     // std::string, std::stoul, std::getline

int main ()
{
  std::string str;
  std::cout << "Enter an unsigned number: ";
  std::getline (std::cin,str);
  unsigned long ul = std::stoul (str,nullptr,0);
  std::cout << "You entered: " << ul << '\n';
  return 0;
}

在这里插入图片描述

std::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);

函数功能
将std::string字符串转换为unsigned long long类型
函数参数

str  待转换的字符串
idx  如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置
base 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0

函数返回值
如果成功则返回转换的unsigned long long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

#include <iostream>   // std::cout
#include <string>     // std::string, std::stoull

int main()
{
	std::string str = "8246821 0xffff 020 -1";

	std::string::size_type sz = 0;   // alias of size_t

	while (!str.empty()) {
		unsigned long long ull = std::stoull(str, &sz, 0);
		std::cout << str.substr(0, sz) << " interpreted as " << ull << '\n';
		str = str.substr(sz);
	}

	return 0;
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/776533.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

为什么计算ORB特征点的时候还要取圆形而不是方形像素区域呢?

ORB (Oriented FAST and Rotated BRIEF)是一种在计算机视觉中广泛应用的特征检测和描述符算法。它的设计目的是为了快速、有效地提取图像中的关键点和描述符。在ORB的过程中&#xff0c;确实会涉及到提取圆形区域的操作&#xff0c;这主要出于以下的原因&#xff1a; 旋转不变性…

C++ VS 链接第三方库

C VS 链接第三方库 include lib dll 需要把动态库考到可执行程序的目录之下

客户异常数据清洗详细教程——pandas

前言 在不同行业中&#xff0c;我们经常会遇到一个麻烦的问题&#xff1a;数据清洗。尤其是当我们需要处理客户编码异常数据时&#xff0c;这个问题变得尤为重要。想象一下&#xff0c;许多银行都是以客户为单位管理数据的&#xff0c;因此每个客户都有一个独特的编码。在处理…

浅谈医院综合电气管理与节能措施

摘要&#xff1a;随着我国经济建设的不断加快&#xff0c;我国能源消耗也越来越大&#xff0c;如何通过电气能源管理实现节能是各行各业都在研究的课题。医院作为我国重要的医疗服务机构&#xff0c;能源管理是医院管理中的重要组成部分。本文将针对医院电气能源管理与节能措施…

关于Qt For android第一次编译时出现的问题

搞了三四天&#xff0c;搞的快崩溃了&#xff0c;问题提示为 FAILURE: Build failed with an exception.* What went wrong: A problem occurred configuring root project android-build. > Could not resolve all artifacts for configuration :classpath.> Could not…

53从零开始学Java之Integer底层原理探究

作者&#xff1a;孙玉昌&#xff0c;昵称【一一哥】&#xff0c;另外【壹壹哥】也是我哦 千锋教育高级教研员、CSDN博客专家、万粉博主、阿里云专家博主、掘金优质作者 前言 在之前的两篇文章中&#xff0c;壹哥给大家介绍了Java中的包装类及其特点、用法&#xff0c;但是这些…

正则表达式——Java

1、简介 正则表达式&#xff08;Regular Expression&#xff09;又称正规表示法、常规表示法&#xff0c;在代码中常简写为 regex、regexp 或 RE&#xff0c;它是计算机科学的一个概念。 String 类里也提供了如下几个特殊的方法。 boolean matches(String regex)&#xff1a…

小研究 - 面向 Java 的高对抗内存型 Webshell 检测技术(四)

由于 Web 应用程序的复杂性和重要性, 导致其成为网络攻击的主要目标之一。攻击者在入侵一个网站后, 通常会植入一个 Webshell, 来持久化控制网站。但随着攻防双方的博弈, 各种检测技术、终端安全产品被广泛应用, 使得传统的以文件形式驻留的 Webshell 越来越容易被检测到, 内存…

视频做成GIF动图怎么做?分享超简单的制作方法

将视频制作GIF动图的好处在于它可以将原本较长的视频压缩成一个简短、易于分享的图像文件。这使得它们非常适合用于社交媒体、博客、电子邮件等场景&#xff0c;可以当做表情包来使用&#xff0c;尤其是看到一段搞笑的视频&#xff0c;想要把它做成GIF动图该怎么做呢&#xff1…

【System Verilog and UVM基础入门17】Using get_next_item()

从小父亲就教育我&#xff0c;做一个对社会有用的人&#xff01; 关于握手协议的文章&#xff0c;网上有很多很多&#xff0c;这篇文章是最原滋原味的介绍&#xff0c;希望可以帮助到有缘人&#xff01; uvm_driver #(REQ,RSP) The base class for drivers that initiate req…

CS 144 Lab One -- 流重组器

CS 144 Lab One -- 流重组器 实验结构如何调试StreamReassembler 实现 对应课程视频: 【计算机网络】 斯坦福大学CS144课程 Lab 1 对应的PDF: Lab Checkpoint 1: stitching substrings into a byte stream 实验结构 这幅图完整的说明了CS144 这门实验的结构&#xff1a; 其中…

MySQL约束和数据类型

目录 约束条件 MySQL数据类型 1、数值类型 2、字符串类型 3、日期时间类型 源码等资料获取方法 约束条件 约束条件就是在给字段加一些约束&#xff0c;使该字段存储的值更加符合我们的预期。 常用约束条件如下&#xff1a; UNSIGNED &#xff1a;无符号&#xff0c;值…

【数据结构与算法】哈夫曼编码(最优二叉树)实现

哈夫曼编码 等长编码&#xff1a;占的位置一样 变长编码&#xff08;不等长编码&#xff09;&#xff1a;经常使用的编码比较短&#xff0c;不常用的比较短 最优&#xff1a;总长度最短 最优的要求&#xff1a;占用空间尽可能短&#xff0c;不占用多余空间&#xff0c;且不…

【MySQL】DML数据操纵语言(非常适合MySQL初学者学习)

&#x1f9d1;‍&#x1f4bb;作者名称&#xff1a;DaenCode &#x1f3a4;作者简介&#xff1a;啥技术都喜欢捣鼓捣鼓&#xff0c;喜欢分享技术、经验、生活。 &#x1f60e;人生感悟&#xff1a;尝尽人生百味&#xff0c;方知世间冷暖。 &#x1f4d6;所属专栏&#xff1a;重…

清华大学携手蚂蚁集团,攻坚可信AI、安全通用大模型等关键技术

2023年4月7日&#xff0c;清华大学与蚂蚁集团签署合作协议&#xff0c;双方将在“下一代互联网应用安全技术”方向展开合作&#xff0c;聚焦智能风控、反欺诈等核心安全场景&#xff0c;携手攻坚可信AI、安全大模型等关键技术&#xff0c;并加速技术落地应用&#xff0c;以解决…

NodeJS内置模块 npm包管理工具 nvm版本管理工具 nrm镜像管理工具

Nodejs 下载 下载地址 node 是什么 node.js 是一个开源的&#xff0c;跨平台的 JavaScript 运行环境 运行 js 文件 node 文件.jsnodemon 监听文件变化 npm i nodemon -gnodemon 文件名全局变量 global globalThis node 中顶级对象为 global &#xff0c;也可以使用 glo…

postgreSQL数据库的安装

文章目录 一、Linux 下安装 postgreSQL 数据库1.1、准备环境1.2、关闭防火墙跟SELinux1.2.1、关闭防火墙 firewalld1.2.2、关闭SELinux 1.3、挂载本地镜像1.4、软件包的下载postgreSQL 一、Linux 下安装 postgreSQL 数据库 1.1、准备环境 操作系统IP应用Red Hat 8192.168.192…

类加载的过程(简单介绍)

目录 一、类加载过程一览 加载&#xff1a; 验证&#xff1a; 准备&#xff1a; 解析&#xff1a; 初始化&#xff1a; 二&#xff1a;类加载器分类 启动类加载器&#xff08;bootstrap class loader&#xff09; 扩展类加载器&#xff08;extensions class loader&…

Nginx外网访问内网如何实现

1、背景 项目要求&#xff1a;将甲方内网的项目能够对外访问&#xff0c;甲方提供一个中间过渡服务器&#xff0c;中间过渡服务器与外网互通&#xff0c;且中间服务器可以访问内网&#xff1b; 外网客户端->中间过渡服务器开放端口&#xff1a;80 中间过渡服务器->内网服…

Cadence Allegro PCB设计88问解析(三十一) 之 Allegro 中 打印(Plot)设置

一个学习信号完整性仿真的layout工程师 在PCB进行投板时&#xff0c;往往会打印一下装备层(Assembly)&#xff0c;给贴片&#xff0c;用于核对器件的信息等。下面简单介绍Allegro中打印(Plot)设置。 1. 在Allegro的菜单下选择File命令&#xff0c;点击Plot Setup&#xff0c;会…