   maps of classes/struct - how do they work?
  ok! I am trying to figure out the map and I have a basic understanding of it. but I can not find how to use classes with them anywhere onlineI want to make a map of a class and then access the data stored in the class through the map. what are some of the methods you use to do this sort of thing?here is my test code:player.h#ifndefPLAYER_H#definePLAYER_Hclass PLAYER{public:int x;int y;};#endifmain.cpp#include "player.h"#include <map>#include <iostream>//3 PLAYER objectsPLAYER P,Q,R;int main(){char close;//give some values to the objects:P.x=1;P.y=2;Q.x=5;Q.y=5;R.x=10;R.y=10;std::map <int, PLAYER> pl_list;pl_list[1] = P;pl_list[2] = Q;pl_list[4] = R;//if 2 does not exist, cout that messageif(pl_list.find(2) == pl_list.end()){    std::cout<<"2 is not in the map!\n";}//go to beginning of map:pl_list.begin();//ok... now how can I access the data inside the class?//pause at end of programstd::cin>>close;return 0;}
  First of all, note that the line:pl_list.begin();Is a no-op. begin() returns an iterator pointing to the first item in the map, and in your code the return value is simply being ignored.As for accessing the values stored in the map, each element of the map is an object of type std::pair<>, which has two member variables called 'first' and 'second'. In the map, 'first' is the key and 'second' is the value. Here is some code that prints out the coordinates of each player in the map:typedef std::map<int,PLAYER>::const_iterator map_itor;for (map_itor itor = players.begin(); itor != players.end(); ++itor) {    std::cout << itor->second.x << " " << itor->second.y << std::endl;}'Hope that helps.   ;  thanks thats what I needed to know... I thought I tried second.x before,but oh well thanks!!   ;  You can also access a specific PLAYER in your map with the [] operator. For example, you can access the PLAYER stored under 2 in your map and change its x to be 12 like this:pl_list[ 2 ].x = 12;Basically, using mapname[ key ] returns whatever object you put in the second member of the map for that particular key.   ;  using the above example to print out all x & y variables, how would I access a function of class PLAYERi get an error message - can not convert from const PLAYER to &PLAYERso I assume it has something to do with the const_iterator   ;Quote:Original post by polpointusing the above example to print out all x & y variables, how would I access a function of class PLAYERi get an error message - can not convert from const PLAYER to &PLAYERso I assume it has something to do with the const_iteratorThe functions probably have to be marked as const, and so not modify the "this" pointer.Either that or change to a regular iterator, not a const one.class Player{public:bool isAlive() const;int getX() const;int getY() const;private:int x,y;};int Player::getX() const{    return x;}int Player::getY() const{    return y;}typedef std::map<int, Player> PlayerMap;int main(){    PlayerMap players;    addLotsTo(players);    for( PlayerMap::const_iterator it = players.begin() ; it != players.end() ; ++it ){  const Player &player = it->second;  std::cout << "Player " << it->first << " x,y " << player .getX() << ',' << player .getY() << std::endl;}};  And just to clarify:When you use a const_iterator, you are essentially making an iterator that can wade through your map forwards and backwards, get info about the members of your map, etc, etc, but not modify any of the objects in your map.If you want an iterator that can modify the objects in your map, simply use iterator instead of const_iterator.Also, when you make a new member function in a class, the compiler assumes that the function modifies the contents of its class, regardless of whether or not it does. However, if you specify const after the argument list, like rip-off showed, than you are telling the compiler that this function does not modify the class it's in. If you don't declare a member function as const, than you're not allowed to call it with a const_iterator.   