简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
1.前言
本篇目的:理解C++之std::get用法。
1.在C++中,std::holds_alternative
是一个模板函数,用于检查给定的std::variant
对象是否包含指定类型的值。它返回一个bool
值,指示给定类型是否存在于std::variant
中。
2.在C++中,std::get
是用于从std::tuple
和std::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: 0
std::cout << "isDouble: " << isDouble << std::endl; // 输出:isDouble: 0
std::cout << "isString: " << isString << std::endl; // 输出:isString: 1
return 0;
}
<2>.v2.0: 对于std::tuple
,std::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; // 输出:42
std::cout << stringValue << std::endl; // 输出:"Hello"
std::cout << doubleValue << std::endl; // 输出:3.14
return 0;
}
<3>.v3.0: 对于std::variant
,std::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);
}