using set.count() return

Discussion in 'C++' started by Gary Wessle, Mar 1, 2007.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    can I use
    if( set.count(key) ) // element exists
    {
    // do something.
    }
    else if( !set.count(key) ) // it does not exist
    {
    // so something else.
    }


    http://www.cppreference.com/cppset/count.html
    count() returns the number of occurrences of key in the set.
    size_type count( const key_type& key );

    well, if set is like a map with values are them selves the keys, and
    the map holds unique keys, then count will always return 1 if the key
    exist and 0 if it does not.

    what am I missing?

    thanks
    Gary Wessle, Mar 1, 2007
    #1
    1. Advertising

  2. Gary Wessle

    Marcus Kwok Guest

    Gary Wessle <> wrote:
    > can I use
    > if( set.count(key) ) // element exists
    > {
    > // do something.
    > }
    > else if( !set.count(key) ) // it does not exist
    > {
    > // so something else.
    > }


    I see no reason why not, though set.find(key) == set.end() (or != as
    appropriate) may indicate your intentions more clearly.

    > http://www.cppreference.com/cppset/count.html
    > count() returns the number of occurrences of key in the set.
    > size_type count( const key_type& key );
    >
    > well, if set is like a map with values are them selves the keys, and
    > the map holds unique keys, then count will always return 1 if the key
    > exist and 0 if it does not.
    >
    > what am I missing?


    In addition to std::set, there is std::multiset, in which case
    set.count() may return a number greater than 1.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Mar 1, 2007
    #2
    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. Greenhorn
    Replies:
    15
    Views:
    815
    Keith Thompson
    Mar 6, 2005
  2. , India
    Replies:
    2
    Views:
    1,073
    James Kanze
    Feb 14, 2008
  3. , India
    Replies:
    7
    Views:
    325
    kwikius
    Apr 28, 2008
  4. Mesvak
    Replies:
    1
    Views:
    297
    Juha Nieminen
    Sep 12, 2008
  5. efelnavarro09
    Replies:
    2
    Views:
    935
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page