I'm trying to assign a float to a defined value in my std::map using std::variant. I initialized my map this way:
std::map<std::string,std::variant<float,int,bool,std::string> kwargs;
kwargs["type"] = "linear";
kwargs["flag"] = true;
kwargs["height"] = 5;
kwargs["length"] = 4.5;
I'm trying to archive this operation:
float f = kwargs["length"];
float a = f+0.5;
How can I transfer std::map key into a float for simple arithmetic operation?
To access the variant, you need to use the free function std::get
:
float f = std::get<float>(kwargs["length"]);
Also, you might run into a problem with this line:
kwargs["length"] = 4.5;
Since 4.5
is a literal of type double
, and not float
. To solve this, just use the F suffix:
kwargs["length"] = 4.5F;