-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvariant.cpp
41 lines (34 loc) · 1.08 KB
/
variant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <vector>
#include <string>
#include <iostream>
#include <variant>
//https://en.cppreference.com/w/cpp/utility/variant
//https://en.cppreference.com/w/cpp/utility/variant/holds_alternative
//https://en.cppreference.com/w/cpp/utility/variant/index
using namespace std;
using MyVariant = std::variant<string, double, vector<float>>;
int main(int argc, char** argv) {
MyVariant var1 = "hello", var2 = 1.23456789, var3 = vector<float>{ 1.2f, 3.4f };
vector<MyVariant> vars = { var1, var2, var3 };
for (const MyVariant& var : vars) {
if (std::holds_alternative<string>(var)) {
cout << std::get<string>(var) << endl;
}
else if (std::holds_alternative<double>(var)) {
cout << std::get<double>(var) << endl;
}
else if (std::holds_alternative<vector<float>>(var)) {
vector<float> vf = std::get<vector<float>>(var);
for (const float& f : vf) {
cout << f << " ";
}
cout << endl;
}
}
/**
* hello
* 1.23457
* 1.2 3.4
**/
return 0;
}