using set.count() return

G

Gary Wessle

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
 
M

Marcus Kwok

Gary Wessle said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top