使用贴图查找向量中的多种模式
所以我已经研究这个模式有一段时间了,并得到了一些帮助来弄清楚如何在这里做到这一点,并认为我可能需要更多的帮助。我已经弄清楚这么多了。
map<int,unsigned> frequencyCount;
//This is my attempt to increment the values of the map everytime one of the same numebers
for(size_t i = 0; i < v.size(); ++i)
frequencyCount[v[i]]++;
unsigned currentMax = 0;
unsigned checked = 0;
unsigned mode = 0;
for(auto it = frequencyCount.cbegin();
it != frequencyCount.cend(); ++it )
if (it ->second > currentMax)
{
mode = it->first;
currentMax = it->second;
}
if (currentMax == 1)
{
cout << "There is no mode in the vector" << endl;
}
else {
cout << " The mode of the vector is: " << mode << endl;
}
因此,
它将返回向量中最常出现的 int,并且如果没有一个映射值超过 1,则返回没有模式。现在我一直在尝试找出在不止一种模式的情况下该怎么办, 前任。 1 2 3 3 4 4 5 当前返回 3。我想返回 3 和 4。
从逻辑上讲,我会说 if 语句检查最频繁出现的变量与第二频繁出现的变量是可行的。当然,如果测试数据足够大的话,400种模式也是有可能的。因此,我需要创建一个循环,当当前变量不再等于出现频率低于它的变量时,该循环将进行检查并退出。我只是真的不知道该怎么做。关于从哪里开始有什么建议吗?
So I've been at this mode thing for a while, and got some help figuring out just how to do it on here and thought I might ask for a little more assitance. I've got this much figured out.
map<int,unsigned> frequencyCount;
//This is my attempt to increment the values of the map everytime one of the same numebers
for(size_t i = 0; i < v.size(); ++i)
frequencyCount[v[i]]++;
unsigned currentMax = 0;
unsigned checked = 0;
unsigned mode = 0;
for(auto it = frequencyCount.cbegin();
it != frequencyCount.cend(); ++it )
if (it ->second > currentMax)
{
mode = it->first;
currentMax = it->second;
}
if (currentMax == 1)
{
cout << "There is no mode in the vector" << endl;
}
else {
cout << " The mode of the vector is: " << mode << endl;
}
}
So it will reutrn the most frequently occuring int within the vector, and return that there is no mode if the none of the map values exceed 1. Now I've been trying to figure out what to in the case of more than one mode, ex. 1 2 3 3 4 4 5 currently returns 3. I would like to reutrn 3 and 4.
Logically I would say an if statement checking the variable which occurs most frequently against the second most frequent would work. Of course there is the possibility of 400 modes if the test data is large enough though. So I would need to create a loop the goes through checking and exits when the current variable is no longer equal to the one that occurs one less frequently than it. I'm just really not sure how to go about that. Any advice on where to get started?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所指出的,该模式不一定是唯一的。
因此,正如您所正确指出的那样,您应该以一组数字的形式提供答案。
我将创建一个向量来存储您的集合中的一组独特模式,然后输出模式向量的内容。
例如
希望这是有道理的
As you point out, the mode is not necessarily unique.
So, you should supply the answer as a set of numbers, as you've correctly stated.
I would create a vector to store the set of unique modes from your set, then output the content of the mode vector.
e.g.
hope this makes sense