Start New Discussion within our Software Development Community

I have a map like this :

map<int,map<int,int> > m;

in this, I can insert value like a 2-D array. like this,

m[i][j]=3;

so, now i want to iterate all the values in a particluar row of map. for ex. if m is like this : m["user-id"]["movie-id"], then i want to iterate on all the values of a particular user-id. as a user-id has some movie-d associated with it in its row (if i consider it as 2-D matrix, although it is not one). Thanks in advance if you can help me. Actually, I wana see that to which movies , a particular user-id has give rating. like a person with user-id 346 has given rating to say movie-id 98, 76, 89, 90. so i wana calculate my answer as 4 as it has rated 4 movies. thanks.

Well if the map is set up like this:

map<"user-id", map<"movie-id", "rating">

Then if you want to see how many movies a person has rated you could do this:

std::map<int, map<int, int> > m;

// add stuff to m

int numberOfMoviesRated = m["some-user-id"].size();

In the above example the call to size calls the size function of the map for that particular user id. If you want to see all of the movies and ratings then you could do this:

std::map<int, map<int, int> > m;

// add stuff to m

for (auto it = m["some-user-id"].begin(); it != m["some-user-id"].end(); ++it)
{
    std::cout << "movie id: " << it->first << "\trating: " << it->second << std::endl;
}
Comments
awesome!! short, concised, well explained!!
This article has been dead for over six months. Start a new discussion instead.