Is there any algorithm included in the STL which allows you to find the frequency (or "mode") of an element in one of the standard containers? I couldn't find any info in the reference; perhaps I was searching for the wrong name.
Thanks in advance.
Printable View
Is there any algorithm included in the STL which allows you to find the frequency (or "mode") of an element in one of the standard containers? I couldn't find any info in the reference; perhaps I was searching for the wrong name.
Thanks in advance.
What about count or count_if?
Code:#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int list[] = {1,2,3,3,1,2,3,5,4,3,2,4,2,3,1};
vector<int> vlist(list, list + 15);
vector<int>::difference_type n(count(vlist.begin(), vlist.end(), 3));
cout<<"There are "<< n <<" 3's in vlist"<<endl;
cin.get();
}
With frequency, do you mean number of occurrences? (I'm not sure what "mode" means in this context)
If so, use the count algorithm:
Code:vector<int> vec = ....
...
//Obtain the frequency of 2
cout << count(vec.begin(),vec.end(),2);
Yes, number of occourences. Thanks to you both for the help.Quote:
Originally posted by Sang-drax
With frequency, do you mean number of occurrences? (I'm not sure what "mode" means in this context)
If so, use the count algorithm:
Code:vector<int> vec = ....
...
//Obtain the frequency of 2
cout << count(vec.begin(),vec.end(),2);