set count() in C++

count(): This function is a build-in function in STL. It finds the number 

of elements. 

This function returns the number of elements with the specified key. 

This function only makes sense for multisets, for set the result will either be 0 

(not present) or 1 (present).

Parameters:

 __x – Element to located.

This function is available in the below file.

File: stl_set.h

Syntax:

st.count(__x)

For Example:

st = {1,2,3,4}

st.count(1) = > It returns 1 (because 1 is present in the set).

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    set<intst;
    st.insert(4);
    st.insert(2);
    st.insert(3);
    st.insert(1);
    st.insert(1);

    cout << st.count(1<< "\n";

    return 0;
}


No comments:

Post a Comment