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.
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(); }
Last edited by Brighteyes; 04-23-2003 at 09:03 AM.
p.s. What the alphabet would look like without q and r.
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);
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Yes, number of occourences. Thanks to you both for the help.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);
Last edited by Eibro; 04-23-2003 at 09:14 AM.