CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: searching a map

  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Question searching a map

    Cna someone please help...I am trying to search a map by a give string "userName" but the map has an int key value

    Status::Code ChatRoomManager::NewChatRoom(ChatRoomInfo* chatRoom_info)
    map<int, ClientInfo*> clients;

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: searching a map

    What is the connection between the map and username ?

  3. #3
    Join Date
    Mar 2010
    Posts
    6

    Re: searching a map

    Sorry username is a bit misguiding. I am trying to search for the chatrooms contained in the map by their name " chat_name"

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: searching a map

    You have no choice but to just iterate thru the map and check
    the chat_name.

    Why do you have an int as the key, if you want to search using
    chatname ?

  5. #5
    Join Date
    Mar 2010
    Posts
    6

    Re: searching a map

    I have to write 2 separate functions, one to search by room ID and one to search by name. What would be the effect of using 2 maps one with string key and the other with an int key?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: searching a map

    Quote Originally Posted by jc35
    I have to write 2 separate functions, one to search by room ID and one to search by name. What would be the effect of using 2 maps one with string key and the other with an int key?
    That can work, but you have to be careful to keep them in sync. Alternatively, you may be able to use Boost.Bimap.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: searching a map

    Another option would be to define a map<string,int> which maps from the name to the integer you need to look up into the other map. The downside is two lookups are required when starting from a name, but in some cases it might be easier to keep things managable than throwing a pointer into two different maps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured