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

Thread: what's the difference bet. std::pair and std::map?

  1. #1
    Join Date
    Jun 2008
    Posts
    11

    Question what's the difference bet. std::pair and std::map?

    What are the instances in which std:air or std::map is used?
    Last edited by SimplySane; June 4th, 2008 at 10:04 PM. Reason: Forgot to put a question mark icon

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: what's the difference bet. std::pair and std::map?

    You use std::pair when you want this

    You use std::map when you want this
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    Re: what's the difference bet. std::pair and std::map?

    I'm not sure exactly what you are asking, but maybe this will help ...

    1) a std::pair is basically a simple struct. Something like:

    Code:
    template <typename T>
    struct pair
    {
        T first;
        T second;
    };
    There is a bit more to it, but that is basically it. You could use it whenever
    you want such a structure.

    2) a std::map is a container that has a key/value pair. You can think
    of it as an ordered binary tree (ordered by the key).

    The type of the elements that go into the conatiner are :
    std::pair<Key,Value>

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

    Re: what's the difference bet. std::pair and std::map?

    A pair has two things. A map has lots of pairs of things. They aren't even vaguely similar.

    A better question would be, what's the difference between a std::set< std:air<T1,T2> > and a std::map<T1,T2>. In which case, the answer is that the first enforces uniqueness on *both* elements of each pair at once, while the second enforces uniqueness only on the first element. In other words, a set<pair> would allow both (5,6) and (5,7), while a map would not.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: what's the difference bet. std::pair and std::map?

    [QUOTE=LindleyIn which case, the answer is that the first enforces uniqueness on *both* elements of each pair at once,...[/QUOTE]

    Just to clarify that part of the post, the former construct enforces the uniqueness of the pair, but does NOT enforce the uniqueness of EITHER member individually.

    To add to this mix consider std::multimap....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)