How to find method of std::map?

Discussion in 'C++' started by Allen, Jan 7, 2009.

  1. Allen

    Allen Guest

    struct FcdaCompare
    {
    bool operator()(const SclNode* n1, const SclNode* n2) const
    {
    return n1 < n2;
    }
    };

    std::map< const SclNode*, const DataSetFcda*, FcdaCompare > fcdaMap;

    I want to find an entry this way

    SclNode * pNode = ...;
    std::map< const SclNode*, const DataSetFcda*, FcdaCompare >::iterator
    itr = fcdaMap.find(pNode); /* find line */
    ....

    But compiling fails at *find line*.
    Where is the problem? Please help me.
    Thank you.

    Allen
     
    Allen, Jan 7, 2009
    #1
    1. Advertising

  2. Allen

    Allen Guest

    On Jan 7, 11:40 am, Allen <> wrote:
    > struct FcdaCompare
    > {
    >         bool operator()(const SclNode* n1, const SclNode* n2) const
    >         {
    >                 return n1 < n2;
    >         }
    >
    > };
    >
    > std::map< const SclNode*, const DataSetFcda*, FcdaCompare > fcdaMap;
    >
    > I want to find an entry this way
    >
    > SclNode * pNode = ...;
    > std::map< const SclNode*, const DataSetFcda*, FcdaCompare >::iterator
    > itr = fcdaMap.find(pNode); /* find line */
    > ...
    >
    > But compiling fails at *find line*.
    > Where is the problem? Please help me.
    > Thank you.
    >
    > Allen


    So bad, the title should be *How to use find method of std::map?*
     
    Allen, Jan 7, 2009
    #2
    1. Advertising

  3. Allen

    Allen Guest

    On Jan 7, 11:40 am, Allen <> wrote:
    > struct FcdaCompare
    > {
    >         bool operator()(const SclNode* n1, const SclNode* n2) const
    >         {
    >                 return n1 < n2;
    >         }
    >
    > };
    >
    > std::map< const SclNode*, const DataSetFcda*, FcdaCompare > fcdaMap;
    >
    > I want to find an entry this way
    >
    > SclNode * pNode = ...;
    > std::map< const SclNode*, const DataSetFcda*, FcdaCompare >::iterator
    > itr = fcdaMap.find(pNode); /* find line */
    > ...
    >
    > But compiling fails at *find line*.
    > Where is the problem? Please help me.
    > Thank you.
    >
    > Allen


    I am so stupid. I code *find line* like this.
    std::map< const SclNode*, const DataSetFcda*, FcdaCompare > itr =
    fcdaMap.find(pNode);
    I feel so sorry!
     
    Allen, Jan 7, 2009
    #3
  4. Allen

    Triple-DES Guest

    On 7 Jan, 04:40, Allen <> wrote:
    > struct FcdaCompare
    > {
    >         bool operator()(const SclNode* n1, const SclNode* n2) const
    >         {
    >                 return n1 < n2;
    >         }
    >
    > };


    This is not a very good idea, because unless n1 and n2 points to
    members of the same object or array, the result of this comparison is
    unspecified. Did you really intend to compare the pointer values? If
    so, you must use
    std::map< const SclNode*, const DataSetFcda* >
    instead.
     
    Triple-DES, Jan 7, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Hildebrand
    Replies:
    5
    Views:
    8,200
    krogers
    Mar 20, 2012
  2. Peter Jansson
    Replies:
    5
    Views:
    6,441
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    456
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,051
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,068
    James Kanze
    Dec 21, 2008
Loading...

Share This Page