I've searched far and wide and can't find an answer. Why can't I access the elements? It gives me the error: "Expression must have a class type". I have no idea what this means. Can someone give me a solution to this please?
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
int main()
{
std::vector<std::map<int, unsigned char>>grid =
{
{std::make_pair(1,'-'), std::make_pair(2,'-'), std::make_pair(3,'-') },
{std::make_pair(4,'-'), std::make_pair(5,'-'), std::make_pair(6,'-') },
{std::make_pair(7,'-'), std::make_pair(8,'-'), std::make_pair(9,'-') }
};
//This doesn't work
std::cout << grid.at(0).at(0).second
}
It seems you mean
instead of
It is the same as
provided that the element with the key
1is the first element in the selected map.That is in the second call of the member function
atyou have to specify a key in the map. In this case the function will return the corresponding value that in your case has the scalar typeunsigned char.The member function
atis declared likeFor a given key it returns the corresponding value.
Within the map at index
0in the vector initialized likekeys are
1,2, and3.So you may use expressions