从堆栈返回最大频率数 [英] Return maximum frequency number from stack
本文介绍了从堆栈返回最大频率数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设计了一个mfc应用程序,其中从udp接收的数据存储在文件和堆栈中。我使用stl进行堆栈;它根据接收顺序存储元素,但我想从堆栈返回重复数字,如何使用堆栈作为函数参数&检索堆栈的元素作为数组?我在下面给出了堆栈中push元素的代码。如果该函数有任何变化,请建议。
I designed one mfc application in which data received from udp is stored in file as well as in stack.I used stl for stack; it stored elements as per receiving order,but i want to return repetitive number from stack,how to use stack as function parameter & retrieve elements of stack as an array? I write code for push elements in stack given below.If any changes in that function please suggest.
template< typename T > void pushElements( T &stackRef,int value)
{
stackRef.push( value);
}
推荐答案
使用此:
Use this:
typedef std::map<int,int> container_t;
void pushElement(container_t &cont, int value)
{
container_t::const_iterator it = cont.find(value);
cont[value] = 1 + (it == cont.end() ? 0: it->second);
}
如果你还需要一个堆栈(用于弹出),你可能需要两个容器:
- 用于弹出的堆栈。
- 频率图(直方图)
这篇关于从堆栈返回最大频率数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文