Solved

Frequency algorithm for STL containers

Posted on 2006-11-21
4
347 Views
Last Modified: 2013-12-14
Is there an STL algorithm to find the element which occurs the most often in a container?
Thanks.
0
Comment
Question by:Rothbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 150 total points
ID: 17988552
No, not to my knowledge.

You could use a multiset though, in combination with the count method :

    size_type count(const key_type& k) const

That will give you the number of elements with a certain value (key). If you repeat that for all values, you can easily extract the max.

That's the first though that comes to mind for your requirements ...
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 150 total points
ID: 17988565
fyi, here's a nice list of the STL algorithms :

http://www.josuttis.com/libbook/algolist.pdf
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 350 total points
ID: 17989557
It is not very difficult to write your own template function, e. g. like the function below.

Regards, Alex


#include <vector>  
#include <iostream>  
#include <algorithm>

template<class I, class T>
int occurs_mostoften(I i1, I i2, T& t)
{
    std::vector<T> v(i1, i2);
    std::sort(v.begin(), v.end());
    int c = 0; int idx = -1; int mc =  0;
    std::vector<T>::iterator il = v.begin();
    std::vector<T>::iterator im = v.begin();
    for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it)
    {
         if (!(*il < *it) && !(*it < *il) )
         {
             if (++c > mc)
             {
                  mc = c;
                  im = il;
             }
         }
         else
         {
              c = 1;
              il = it;
          }    
    }

    if (im != v.end()) t = *im;
    return mc;
}


int main()
{
    int a[] = { 4, 6, 3, 4, 2, 8, 3, 2, 3, 1, 0, 4, 1, 3, 2, 9, 7, 3 };
    vector<int> v(&a[0], &a[sizeof(a)/sizeof(int)]);
    vector<int>::iterator it;
    int c, r = -1;
    if ((c = occurs_mostoften(v.begin(), v.end(), r)) > 0)
    {
        std::cout << r << " : " << c << endl;
    }
    return 0;
}

0
 

Author Comment

by:Rothbard
ID: 18005090
Thanks to all who replied!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 316
Best book to learn C++ 4 97
How do I get Window Title of all opened process? 4 61
parameter pack in c++11 2 23
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question