C++ 如何使用C+中的频率表计算模式+;

C++ 如何使用C+中的频率表计算模式+;,c++,statistics,C++,Statistics,我的任务是打印数据的模式(最频繁的值),我已经编写了代码来告诉哪个值出现了多少次,但在我的输出之后,它还显示了一些随机数,我似乎不知道为什么 //计算和显示模式 对于(i=0;i而言,这是一个问题,在这种情况下被问得更频繁 计算数组中元素的频率。标准方法是使用std::map,将数组的值存储在其中,并为每个相关值增加相应的插槽 最大频率可通过STL std算法std::max_element找到 所以基本上我们需要两个陈述来回答这个问题 请参阅以下众多解决方案建议之一: #include <

我的任务是打印数据的模式(最频繁的值),我已经编写了代码来告诉哪个值出现了多少次,但在我的输出之后,它还显示了一些随机数,我似乎不知道为什么

//计算和显示模式

对于(i=0;i而言,这是一个问题,在这种情况下被问得更频繁

计算数组中元素的频率。标准方法是使用
std::map
,将数组的值存储在其中,并为每个相关值增加相应的插槽

最大频率可通过STL std算法
std::max_element
找到

所以基本上我们需要两个陈述来回答这个问题

请参阅以下众多解决方案建议之一:

#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>

std::vector<int> arr{1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,8,8,8,8,8,8,8,8,6,6,6,6,6,6,7,7,7,7,7,7,7};

int main()
{
    // Here we will store the frequency for each element 
    std::map<int, size_t> frequency;

    // Make reading easier
    using pair_t = decltype(frequency)::value_type;

    // Count frequency
    std::for_each(arr.begin(), arr.end(), [&frequency](int& i){ frequency[i]++; });

    // Output highes frequency of array value
    std::cout << "\nMax:  " << std::max_element(frequency.begin(), frequency.end(), []
        (const pair_t& p1, const pair_t& p2){ return p1.second < p2.second;})->first << "\n";

    return 0;
}
#包括
#包括
#包括
#包括
#包括
向量arr{1,2,2,3,3,3,4,4,4,5,5,5,5,8,8,8,8,6,6,6,6,7,7,7,7,7,7};
int main()
{
//这里我们将存储每个元素的频率
标准:映射频率;
//使阅读更容易
使用pair\u t=decltype(频率)::value\u type;
//计数频率
std::for_each(arr.begin()、arr.end()、[&frequency](int&i){frequency[i]+;});
//输出阵列值的高频率

std::cout所以基本上你有一个整数数组,你想打印数组中每个元素的频率以及模式?你能提供你的数组吗?一种方法是对数组进行排序,然后寻找相同数字的最长序列。