C++ set count
The count function in C++ set is employed to retrieve the count of elements within the container. As sets in C++ do not allow duplicate elements, this function will return 1 when the specified value 'val' is found in the set, and 0 if it is not present.
Syntax
size_type count (const value_type& val) const;
Parameter
val : Value to be searched in the set container.
Return value
It returns 1 if the element containing the value val exists within the set container; otherwise, it returns 0.
Complexity
Logarithmic in size.
Iterator validity
No changes.
Data Races
The container is accessed.
Simultaneously retrieving the elements of a set is considered to be a secure operation.
Exception Safety
If an error is raised, the container remains unchanged.
Example 1
Let's explore a basic example demonstrating how to locate an element based on a specified key value:
#include <iostream>
#include <set>
using namespace std;
int main()
{
// initialize container
set<int> mp;
// insert elements in random order
mp.insert(30);
mp.insert( 40 );
mp.insert( 60 );
mp.insert( 20);
mp.insert( 50 );
// checks if key 30 is present or not
if (mp.count(30))
cout << "The key 30 is present\n";
else
cout << "The key 30 is not present\n";
// checks if key 100 is present or not
if (mp.count(100))
cout << "The key 100 is present\n";
else
cout << "The key 100 is not present\n";
return 0;
}
Output:
The key 30 is present
The key 100 is not present
In the example provided, the count method verifies the presence of a specified value. If the element exists within the set container, a message indicating its presence is shown; otherwise, it indicates that the element is not present.
Example 2
Let's explore a basic example of searching for the elements within a set:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<char> myset;
char c;
myset = {'a', 'c', 'f'};
for (c='a'; c<'h'; c++)
{
cout << c;
if (myset.count(c)>0)
cout << " is an element of myset.\n";
else
cout << " is not an element of myset.\n";
}
return 0;
}
Output:
a is an element of myset.
b is not an element of myset.
c is an element of myset.
d is not an element of myset.
e is not an element of myset.
f is an element of myset.
g is not an element of myset.
In the given example, the count method is employed to locate the elements 'a' through 'h' within the set.
Example 3
Let's explore a basic example of searching for keys within a set:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<char> m = {'a','b','c','d'};
if (m.count('a') == 1) {
cout<< " 'a' is present in the set \n";
}
if (m.count('z') == 0) {
cout << " 'z' is not present in the set" << endl;
}
return 0;
}
Output:
'a' is present in the set
'z' is not present in the set
In the given illustration, the element 'a' is found within the set 'm', thus it will serve as the value for 'a'. Conversely, since the element 'z' is not contained within the set, there is no corresponding value for 'z'.
Example 4
Let's see a simple example:
#include <set>
#include <iostream>
int main()
{
using namespace std;
set<int> s1;
set<int>::size_type i;
s1.insert(1);
s1.insert(1);
// Keys must be unique in set, so duplicates are ignored
i = s1.count(1);
cout << "The number of elements in s1 with a sort key of 1 is: "
<< i << "." << endl;
i = s1.count(2);
cout << "The number of elements in s1 with a sort key of 2 is: "
<< i << "." << endl;
}
Output:
The number of elements in s1 with a sort key of 1 is: 1.
The number of elements in s1 with a sort key of 2 is: 0.