Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does STL set have count() when all elements are supposed to be unique?

Tags:

c++

set

stl

I can understand that multiset has count(), for counting the number of occurrences of a value, because elements can be repeated in multiset. But what's the point of having count() in set, when all the values are already unique?

like image 387
Nav Avatar asked Dec 03 '10 06:12

Nav


People also ask

What is unique about elements of a set in C++ STL?

Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending.

What does count function do in set?

The COUNT function counts the number of cells that contain numbers, and counts numbers within the list of arguments. Use the COUNT function to get the number of entries in a number field that is in a range or array of numbers.

What is the purpose of count in C++?

The set::count() is a built-in function in C++ STL which returns the number of times an element occurs in the set. It can only return 1 or 0 as the set container contains unique elements only. Parameters: The function accepts one mandatory parameter element which specifies the element whose count is to be returned.

Does set allow duplicates in C++?

Properties of set in C++ The property of Uniqueness: Every element of a set in C++ must be unique, i.e., no duplicate values are allowed.


1 Answers

count is part of the associative container requirements(1).

Every associative container is required to provide it as part of its interface, even if the result is always zero or one as is the case with std::set.


(1) This is a link to the SGI STL documentation describing the Associative Container concept; the concept as defined in the C++ standard may differ slightly, but not substantially.

like image 61
James McNellis Avatar answered Oct 05 '22 04:10

James McNellis