I have declared and populated a map : std::map<std::string, std::vector<std::pair<std::string, std::vector<std::vector<float>>>>> data;.
I have a print function to print the contents of the map which is as follows :
void print() {
for (auto it : data) {
std::cout << it->first << "\n";
for (auto iter : it->second) {
std::cout << iter->first << "\n";
for (auto val : iter->second) {
std::cout << val->second << "";
}
std::cout << "\n";
}
std::cout << "\n";
}
}
But, when I compile the program, I get the following error :
operator -> or ->* applied to "std::pair<const std::string,
std::vector<std::pair<std::string,
std::vector<std::vector<float,
std::allocator<float>>,
std::allocator<std::vector<float, std::allocator<float>>>>>,
std::allocator<std::pair<std::string,
std::vector<std::vector<float, std::allocator<float>>,
std::allocator<std::vector<float, std::allocator<float>>>>>>>>"
instead of to a pointer typeC/C++(3364)
I don't know what I'm doing wrong.
I also tried accessing the map data through (*it).first, which throws the following error :
no operator "*" matches these operandsC/C++(349)
myfile.cpp(80, 23): operand types are: * std::pair<const std::string,
std::vector<std::pair<std::string, std::vector<std::vector<float,
std::allocator<float>>, std::allocator<std::vector<float,
std::allocator<float>>>>>, std::allocator<std::pair<std::string,
std::vector<std::vector<float, std::allocator<float>>,
std::allocator<std::vector<float, std::allocator<float>>>>>>>>
What should I do instead of what I have did? Please let me know if I'm wrong anywhere in the code.
While trying to unroll your map, you made several mistakes. You need to go from outer to inner structures.
And you need to understand that using a range based for loop already unrolls one level.
In order to learn it, you should avoid to use the
autokey word, because then you must name the correct data type.All the above will lead to:
Additionally, to define your own types with the
usingstatement will also be helpful. Then you can first build your data structure from inside out. And, for printing, you can use the predefined types.This will make life a little bit simpler:
And, last but not least, you can use structured bindings for
std::mapandstd::pair, this time withautoagain, but again, a bit more readable:Unfortunately, the complicated data structure will make it a bit difficult to access sub-elements.