I want to add, edit, and read content in a nested vector and map
typedef std::map<std::string, std::any> T_map;
typedef std::vector<std::map<std::string, std::any>> T_vector;
T_map data;
data["Total"] = 50;
data["Name"] = std::string {"abc123"};
int Total = std::any_cast<int>(data["Total"]);//read
std::string Named = std::any_cast<std::string>(data["Name"]);//read
data["Functions"] = T_vector(50); //initialized with size 50
std::any_cast<T_map>(std::any_cast<T_vector>(data["Functions"])[0])["addr"] = 123456; //There are no data changes
int addr = std::any_cast<int>(std::any_cast<T_map>(std::any_cast<T_vector>(data["Functions"])[1])["addr"]); //read error
I think this error is because adding information in line 11 was not as expected, leading to an error in the code in line 12.
terminate called after throwing an instance of ‘std::bad_any_cast’
what(): bad any_cast
Is there any other solution or alternative that is more understandable?
New contributor
Ten Tatsu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.