C ++寻找的元素具有最高occurence在一个数组数组、在一、元素、最高

2023-09-11 02:21:21 作者:热情喂冷风

我在寻找确定哪些元素具有最高的发生(模式)在C ++ PTR阵列的一个优雅的方式。

例如,在

  {鸭梨,苹果,橙色,苹果}
 

苹果元素是最常见的。

我的previous尝试都失败 编辑:该数组已排序

  INT获取模式(INT *学生,诠释大小)
{
    INT模式;
    诠释计数= 0,
    MAXCOUNT = 0,
    preVAL;

    preVAL =学生[0]; // preVall认为被比较的电流模式数
    数= 1;
    的for(int i = 0; I<大小;我++)//检查每个数字阵列中
    {
        如果(学生[我] == preVAL)//检查是否当前模式再看
        {
            算上++; //次电流模式数目之金额已经看到。
            如果(MAXCOUNT&所述;计数)//如果次模式已被看过的量多于MAXCOUNT
            {
                MAXCOUNT =计数; //已看到较大它模式现在是MAXCOUNT
                模式=学生[I] //当前阵列项目将成为模式
            }其他{
                preVAL =学生[I]
                数= 1;
            }

        }

    }

    返回模式;
}
 
在asp中有一个数组,数组里面的元素都是如下所示

解决方案

有几种可能的解决方案的问题,但首先一些建议: 不要使用C风格的数组。使用的std ::阵列的固定(编译时)大小的数组或的std ::矢量有关堆阵列(或C ++ 14的的std ::的DynArray 如果数组的大小在运行时确定,但创建后不会改变)。这些容器做内存管理,你,你不需要单独周围传递数组的大小。除了使用的容器,preFER使用算法<算法> 哪里不合适。如果你不知道的容器和算法,需要一些时间来熟悉他们,到时候会很快还清。

所以,这里有一些解决方案草图:

对数组进行排序,然后计算连续值的ocurrences。它更容易比跟踪哪些值,你已经算,哪些不是。你基本上只需要两个价值数对:一个是当前正在计算的价值,一个是最大计数到现在。您只需要五分之一的变量:迭代器的容器

如果您不能排序的数组或需要跟踪的所有的数量,使用贴图值映射到其发生在数组中的号码。如果您熟悉的std ::地图,这是非常简单的事情。最后,搜索的最大数量,即在最大映射值:

 为(自动I:学生)countMap [I] ++;
汽车POS =的std :: max_element(开始(countMap),端(countMap)
  [](自动LHS,汽车右){返回lhs.second< rhs.second}); //!见下文
汽车MAXCOUNT = POS->第二个;
 

注:本使用C ++ 11的射程为基础和C ++ 14多态LAMBDA。它应该是显而易见的东西都是在这里完成,因此它可以被调整为C ++ 11 / C ++ 14的支持你的编译器提供。

I'm looking for an elegant way of determining which element has the highest occurrence (mode) in a C++ ptr array.

For example, in

{"pear", "apple", "orange", "apple"}

the "apple" element is the most frequent one.

My previous attempts have failed EDIT: The array has already been sorted.

int getMode(int *students,int size)
{
    int mode;
    int count=0, 
    maxCount=0,
    preVal;

    preVal=students[0]; //preVall holds current mode number being compared
    count=1;
    for(int i =0; i<size; i++) //Check each number in the array
    {
        if(students[i]==preVal) //checks if current mode is seen again
        {
            count++; //The amount of times current mode number has been seen.
            if(maxCount<count)  //if the amount of times mode has been seen is more than maxcount
            {
                maxCount=count; //the larger it mode that has been seen is now the maxCount
                mode=students[i]; //The current array item will become the mode
            }else{
                preVal = students[i];
                count = 1;
            }

        }

    }

    return mode; 
}

解决方案

There are several possible solutions to that problem, but first some advice: Don't use C-style arrays. Use std::array for fixed (compiletime) size arrays or std::vector for arrays on the heap (or C++14's std::dynarray if the array size is determined at runtime but does not change after creation). Those containers do the memory management for you, and you do not need to pass the array size around separately. In addition to using containers, prefer to use the algorithms in <algorithm> where appropiate. If you don't know the containers and algorithms, take some time to get familiar with them, that time will pay off very soon.

So, here are some solution sketches:

Sort the array, then count the ocurrences of consecutive values. It's much easier than to keep track of which values you have already counted and which not. You basically need only two value-count pairs: one for the value you are currently counting, one for the maximum count up to now. You will only need a fifth variable: the iterator for the container.

If you cannot sort your array or need to keep track of all counts, use a map to map values to their number of occurrence in the array. If you are familiar with std::map, that is very simple to do. At the end, search for the maximum count, i.e. for the maximum map value:

for (auto i: students) countMap[i]++;
auto pos = std::max_element(begin(countMap), end(countMap), 
  [](auto lhs, auto rhs){ return lhs.second < rhs.second }); //! see below
auto maxCount = pos->second;

Note: this uses C++11's range based for and a C++14 polymorphic Lambda. It should be obvious what is done here, so it can be adjusted for the C++11/C++14 support your compiler provides.