finding/counting elements in a STL::map

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael H Lees

    #1

    finding/counting elements in a STL::map

    Hi all,

    As I see it there are two options for checking if for an element with
    key 'x' exists in a map m....

    1.) if(m.find(x)!=m .end())

    or

    2.) if(m.count(x))


    They both take O(log N) where N is number of elements. Obviously they
    both provide different things but which method should you use for just
    checking if a key exists? Does it in fact matter? I have seen examples
    of both


    Thanks

    -Mike

  • Victor Bazarov

    #2
    Re: finding/counting elements in a STL::map

    "Michael H Lees" <mhl@cs.nott.ac .uk++> wrote...[color=blue]
    > As I see it there are two options for checking if for an element with
    > key 'x' exists in a map m....
    >
    > 1.) if(m.find(x)!=m .end())
    >
    > or
    >
    > 2.) if(m.count(x))
    >
    >
    > They both take O(log N) where N is number of elements. Obviously they
    > both provide different things but which method should you use for just
    > checking if a key exists? Does it in fact matter? I have seen examples
    > of both[/color]

    It doesn't matter. IMHO, if it's a map<>, where keys are single,
    using 'count' is a bit counterintuitiv e (pun intended). Use whatever
    you find more attractive (pun intended).

    Victor


    Comment

    • David Cattarin

      #3
      Re: finding/counting elements in a STL::map

      "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<vflubl982 nca0f@corp.supe rnews.com>...[color=blue]
      > "Michael H Lees" <mhl@cs.nott.ac .uk++> wrote...[color=green]
      > > As I see it there are two options for checking if for an element with
      > > key 'x' exists in a map m....
      > >
      > > 1.) if(m.find(x)!=m .end())
      > >
      > > or
      > >
      > > 2.) if(m.count(x))
      > >
      > >
      > > They both take O(log N) where N is number of elements. Obviously they
      > > both provide different things but which method should you use for just
      > > checking if a key exists? Does it in fact matter? I have seen examples
      > > of both[/color]
      >
      > It doesn't matter. IMHO, if it's a map<>, where keys are single,
      > using 'count' is a bit counterintuitiv e (pun intended). Use whatever
      > you find more attractive (pun intended).[/color]

      LOL. That's pretty cute.

      However, I'd say use whatever you think other people would find
      intuitive. If there is any possiblility that someone else will have to
      maintain your code, take pitty on the poor slob and use find. Odds are
      that using count would just confuse other people.

      Dave

      Comment

      • Samuele Armondi

        #4
        Re: finding/counting elements in a STL::map


        "Michael H Lees" <mhl@cs.nott.ac .uk++> wrote in message
        news:bdeqoj$mv8 $1@oyez.ccc.not tingham.ac.uk.. .[color=blue]
        > Hi all,
        >
        > As I see it there are two options for checking if for an element with
        > key 'x' exists in a map m....
        >
        > 1.) if(m.find(x)!=m .end())
        >
        > or
        >
        > 2.) if(m.count(x))
        >
        >
        > They both take O(log N) where N is number of elements. Obviously they
        > both provide different things but which method should you use for just
        > checking if a key exists? Does it in fact matter? I have seen examples
        > of both
        >
        >
        > Thanks
        >
        > -Mike
        >[/color]
        I would stick to find(), as it conveys the meaning of what you are trying to
        do. It will save some poor maintenance coder scratching his head over coffee
        for 2 hours!
        S. Armondi


        Comment

        Working...