Thread: Help me figure out how to use STL map containers

  1. #1
    Registered User
    Join Date
    Aug 2009

    Help me figure out how to use STL map containers

    edit: oops, solved it on my own
    turns out SOCKET is an int anyway.


    Hi, I have encountered the first time I need to use the STL map container and it's confusing me.

    Firstly I will describe the situation:
    I am programming a simple winsock2 server.
    My client objects contain the socket objects. (apart from the listener)
    My client objects are stored in a STL list contained in the core server object.

    I am looping through the fd_set outputted by select() and I must find a way to reference the client objects from the socket objects in the fd_set member array fd_array. I believe I need to use a hash table to do so, hence why I need to understand maps. I wouldn't be having problems if it was a simple map of normal data types, but things seem to get confusing if you want anything more than just ints/floats/longs etc. I believe the map must be defined as follows:

    std::map<SOCKET,std::list<ribClient*>::iterator> sockMap;

    (except with a 3rd argument for the comparison function that I have to make)
    That will allow me to find the list iterator for the client which contains the SOCKET, I think.
    What confuses me is how I make the comparison function; I have been reading about maps and it seems my comparison function must output whether one SOCKET is lower than the other... how is it possible for a SOCKET to have a higher or lower value than any other SOCKET? Am I doing this totally wrong or have I encountered a genuine problem to overcome?

    Also, how does the insert argument for position work? I understand that it is a map iterator, but where is the best place to have it pointing to?

    Thanks for the help.
    Last edited by Bozebo; 01-07-2011 at 03:55 PM. Reason: oops, solved

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    How do you think map would compare two pointers?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You don't need to use the version of insert that takes an iterator, it's only an optimisation hint, which isn't useful in this case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Map Of Vectors
    By Jonnster in forum C++ Programming
    Replies: 9
    Last Post: 11-17-2010, 02:52 PM
  2. STL map comparing keys
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2004, 10:32 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. STL Map
    By dpsalchl in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2002, 01:02 PM
  5. Using the STL map for one's own class
    By SilentStrike in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2001, 08:06 PM