C++ STL set, count() code example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the C++ count() to return the number of elements in a set whose key matches a parameter-specified key in C++ programming
To show: How to use the C++ STL set, count() to return the number of elements in a set whose key matches a parameter-specified key in C++ programming
// set, count()
#include <set>
#include <iostream>
using namespace std;
int main(void)
{
// set container
set <int> st1;
size_t i;
// insert data
st1.insert(1);
st1.insert(2);
st1.insert(1);
// keys must be unique in set, duplicates are ignored
i = st1.count(1);
cout<<"The number of elements in st1 set with a sort key of 1 is: "<<i<<endl;
i = st1.count(2);
cout<<"The number of elements in st1 set with a sort key of 2 is: "<<i<<endl;
i = st1.count(3);
cout<<"The number of elements in st1 set with a sort key of 3 is: "<<i<<endl;
return 0;
}
Output examples:
The number of elements in st1 set with a sort key of 1 is: 1
The number of elements in st1 set with a sort key of 2 is: 1
The number of elements in st1 set with a sort key of 3 is: 0
Press any key to continue . . .