C++之std::holds_alternative与std::get应用实例(二百一十九)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!

优质专栏:Audio工程师进阶系列原创干货持续更新中……】🚀

人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

更多原创,欢迎关注:Android系统攻城狮

欢迎关注Android系统攻城狮

1.前言

本篇目的:理解C++之std::get用法。

1.在C++中,std::holds_alternative是一个模板函数,用于检查给定的std::variant对象是否包含指定类型的值。它返回一个bool值,指示给定类型是否存在于std::variant中。
2.在C++中,std::get是用于从std::tuplestd::variant等类型中获取值的函数模板。它接受一个索引作为参数,并返回相应位置的值。

2.应用实例

<1>.v1.0:std::holds_alternative用法

#include <iostream>
#include <variant>
#include <string>int main() {std::variant<int, double, std::string> myVariant = "Hello";// 使用std::holds_alternative检查指定类型是否存在于variant中bool isInt = std::holds_alternative<int>(myVariant);bool isDouble = std::holds_alternative<double>(myVariant);bool isString = std::holds_alternative<std::string>(myVariant);std::cout << "isInt: " << isInt << std::endl;            // 输出:isInt: 0std::cout << "isDouble: " << isDouble << std::endl;      // 输出:isDouble: 0std::cout << "isString: " << isString << std::endl;      // 输出:isString: 1return 0;
}

<2>.v2.0: 对于std::tuplestd::get的用法。

#include <iostream>
#include <tuple>int main() {std::tuple<int, std::string, double> myTuple(42, "Hello", 3.14);// 使用std::get获取tuple中的值int intValue = std::get<0>(myTuple);std::string stringValue = std::get<1>(myTuple);double doubleValue = std::get<2>(myTuple);std::cout << intValue << std::endl;        // 输出:42std::cout << stringValue << std::endl;     // 输出:"Hello"std::cout << doubleValue << std::endl;     // 输出:3.14return 0;
}

<3>.v3.0: 对于std::variantstd::get的用法。

#include <iostream>
#include <variant>
#include <string>int main() {std::variant<int, std::string, double> myVariant = 42;// 使用std::get获取variant中存储的值int intValue = std::get<int>(myVariant);std::cout << intValue << std::endl;        // 输出:42// 尝试使用std::get获取不匹配的类型,会抛出std::bad_variant_access异常try {std::string stringValue = std::get<std::string>(myVariant);} catch(const std::bad_variant_access& e) {std::cout << "Error: " << e.what() << std::endl;  // 输出:Error: std::bad_variant_access}return 0;
}

<4>.v4.0

#include <iostream>
#include <variant>
#include <string>
#include <utility>using namespace std;using Elem = std::variant<std::monostate, int32_t, int64_t, double, std::string, std::pair<int64_t, int64_t>>;int main() {Elem e1 = 42;Elem e2 = "Hello, world!";Elem e3 = 3.14;Elem e4 = std::make_pair(10, 20);if (std::holds_alternative<int32_t>(e1)) {int32_t value = std::get<int32_t>(e1);cout << "e1 is an int32_t: " << value << endl;}if (std::holds_alternative<std::string>(e2)) {std::string value = std::get<std::string>(e2);cout << "e2 is a string: " << value << endl;}if (std::holds_alternative<double>(e3)) {double value = std::get<double>(e3);cout << "e3 is a double: " << value << endl;}if (std::holds_alternative<std::pair<int64_t, int64_t>>(e4)) {std::pair<int64_t, int64_t> value = std::get<std::pair<int64_t, int64_t>>(e4);cout << "e4 is a pair: (" << value.first << ", " << value.second << ")" << endl;}return 0;
}

<5>.v5.0

#include <iostream>
#include <string>
#include<typeinfo>
#include <variant>
#include <map>using namespace std;class Item {
public:template<typename S, typename T>Item &set(S key, T value) {printf("xxx---------->%s(), line = %d, key = %s, vlaue = %d\n",__FUNCTION__,__LINE__,key,value);findOrAllocateProp(key).set(value);return *this;}Item &setInt32(const char *key, int32_t value) {printf("xxx---------->%s(), line = %d, key = %s, vlaue = %d\n",__FUNCTION__,__LINE__,key,value);return set(key, value);}class Prop {public:using Elem = std::variant<std::monostate, int32_t, int64_t, double, std::string, std::pair<int64_t, int64_t>>;//Prop() = default;void setName(const char *name) {printf("xxx---------->%s(), line = %d, name = %s\n",__FUNCTION__,__LINE__,name);mName = name;}template <typename T> void set(const T& value) {//printf("xxx---------->%s(), line = %d,typeid(value) = %s\n",__FUNCTION__,__LINE__,typeid(value).name());printf("xxx---------->%s(), line = %d, value = %d\n",__FUNCTION__,__LINE__,value);mElem = value;//获取mElem中的int类型的值.int32_t val = std::get<int32_t>(mElem);printf("xxx---------->%s(), line = %d,val = %d\n",__FUNCTION__,__LINE__,val);}private:std::string mName;Elem mElem;};Prop &findOrAllocateProp(const char *key) {auto it = mProps.find(key);//printf("xxx---------->%s(), line = %d, key = %s, vlaue = %d\n",__FUNCTION__,__LINE__,key,it->first);printf("xxx---------->%s(), line = %d, key = %s\n",__FUNCTION__,__LINE__,key);if (it != mProps.end())return it->second;Prop &prop = mProps[key];prop.setName(key);return prop;}std::map<std::string, Prop> mProps;
};int main(){Item it;it.setInt32("Tom", 18);}

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

梓航建站系统独立开源版企业官网建站v1.2.5

1、超级伪静态功能便于SEO 2、自定义导航菜单内容及连接(支持三级分类) 3、多种模型自由组合&#xff0c;灵活度更高 4、页面自由控制数据显示(分类更清晰) 5、43种图文布局让页面更酷炫 6、8种文章布局展示(文字也能多样化) 7、4种留言布局&#xff0c;表单也不单调了 …

初识Docker

文章目录 Docker安装Docker简介1.什么是虚拟化、容器化?2. 为什么需要虚拟化、容器化&#xff1f;3. 虚拟化的实现方式主机虚拟化的实现方式容器虚拟化实现 4. 虚拟机和Docker的区别 Docker安装 基于Centos7进行安装 1.确认系统版本和CPU架构&#xff0c;Centos7的x86_64架构…

【Selenium】webdriver.ChromeOptions()官方文档参数

Google官方Chrome文档&#xff0c;在此记录一下 Chrome Flags for Tooling Many tools maintain a list of runtime flags for Chrome to configure the environment. This file is an attempt to document all chrome flags that are relevant to tools, automation, benchm…

从实时监控到智能洞察:Grafana 和 CnosDB 的无限潜力

在今天的数字化世界中&#xff0c;监控系统对于维护应用程序和基础设施的稳定性至关重要。本文将介绍如何使用 Grafana 和 CnosDB 构建强大的监控体系&#xff0c;以便实时监视性能、发现问题并采取及时的措施。 CnosDB已正式上架Grafana插件市场 Grafana&#xff1a;开源监控和…

(手撕)数据结构--->堆

文章内容 目录 一&#xff1a;堆的相关概念与结构 二&#xff1a;堆的代码实现与重要接口代码讲解 让我们一起来学习:一种特殊的数据结构吧&#xff01;&#xff01;&#xff01;&#xff01; 一&#xff1a;堆的相关概念与结构 在前面我们已经简单的学习过了二叉树的链式存储结…

虚拟化和容器

文章目录 1 介绍1.1 简介1.2 虚拟化工作原理1.3 两大核心组件&#xff1a;QEMU、KVMQEMUKVM 1.4 发展历史1.5 虚拟化类型1.6 云计算与虚拟化1.7 HypervisorHypervisor分为两大类 1.8 虚拟化 VS 容器 2 虚拟化应用dockerdocker 与虚拟机的区别 K8Swine 参考 1 介绍 1.1 简介 虚…

stm32---定时器输入捕获

一、输入捕获介绍 在定时器中断实验章节中我们介绍了通用定时器具有多种功能&#xff0c;输入捕获就是其中一种。 STM32F1除了基本定时器TIM6和TIM7&#xff0c;其他定时器都具有输入捕获功能 。输入捕获可以对输入的信号的上升沿&#xff0c;下降沿或者双边沿进行捕获&#xf…

Linux中swap几乎耗尽,但物理内存还有空余的现象

故障现象&#xff1a; 产生此现象的原因&#xff1a; swappiness 配额设置了偏高的值。 还有一个潜在的因素是某个程序因其自身对内存管理的缺陷&#xff0c;形成了zombie进程、且为及时关闭的处理任务还在持续消耗Mem及swap。 解决办法&#xff1a; 调低swappiness 配额值&…

C++ - 异常介绍和使用

前言 我们在日常编写代码的时候&#xff0c;难免会出现编写错误带来程序的奔溃&#xff0c;或者是用户在使用我们编写的程序时候&#xff0c;使用错误所带来程序的奔溃。 在C 当中 可以对你觉得可能发生 错误 的地方在运行之前进行判断&#xff0c;发生错误可以给出提示。 C…

PDF 工具箱

PDF 工具箱 V9.0.0.1 程序&#xff1a;VB.net 运行库&#xff1a;NET Framework 4.5 功能简介&#xff1a; 1、PDF文件多文件合并&#xff0c;可调整顺序。 2、PDF文件拆分&#xff0c;将每页拆分成独立的PDF文件。 3、PDF文件添加水印&#xff0c;文字或图片水印&…

解决中国科大 USTC 邮箱系统的超大附件上传的邮箱控件安装问题

USTC邮箱系统上传超过 48M 的附件的步骤&#xff1a; 从文件中转站上传文件&#xff0c;会提示下载邮箱控件 cmplugin_setup.exe &#xff0c;默认安装C盘即可 2. 安装好之后依然无法上传超大文件&#xff0c;因为只有 IE 浏览器支持该功能&#xff0c;所以可以使用 Edge 浏览…

opencv dnn模块 示例(16) 目标检测 object_detection 之 yolov4

博客【opencv dnn模块 示例(3) 目标检测 object_detection (2) YOLO object detection】 测试了yolov3 及之前系列的模型&#xff0c;有在博客【opencv dnn模块 示例(15) opencv4.2版本dnn支持cuda加速&#xff08;vs2015异常解决&#xff09;】 说明了如何使用dnn模块进行cuda…