How would I access elements from the following map:
map<int, string[4]> * my_map;
I used to do it through the at() operator
string * val_ptr = my_map->at(key);
Recently, I have discovered that this is a non standard feature of my compiler and the correct way of doing it is through operator[]. Unfortunately, the compiler keeps trying to convert my key to string [4]:
string * val_ptr = my_map->operator[](key);
error: conversion from ‘int’ to non-scalar type ‘std::string [4]’ requested
I have looked online, but there don’t seem to be any examples with a map of string arrays. Am I doing something invalid? Should I be using a vector instead, and if so, would it be slower to create and access?
Use of
.at()function is not non-Standard anymore. It is in the Standard C+11 (see doc).Now, this,
which is correct, but it should be written as:
as it is more succinct.
As for the compiler error, it is somewhere else.
In fact, I believe, the problem is coming from somewhere, and caused by pointer declaration of the map. Why don’t you declare the map as:
and then use
Even better if you use
std::vector:and then use