Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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.
C++
template< typename T > void pushElements( T &stackRef,int value)
{
       stackRef.push( value);
}
Posted
Comments
Richard MacCutchan 17-Dec-13 5:32am    
Don't use a stack, use one of the other collection classes so you can sort and count.
Venkat Raghvan 17-Dec-13 6:07am    
which collection class i should used?
Richard MacCutchan 17-Dec-13 6:43am    
Whichever one provides all the features that you want to use. If you don't care about duplicates then vector should be OK.

1 solution

Use this:
C++
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); 
}


If you also need a stack (for popping), you may need two containers:

  • A stack for popping.
  • A map for frequencies (histogram)


In this case, consider encapsulating both containers in a class of your own.

Hope this helps,
 
Share this answer
 
v3
Comments
Richard MacCutchan 17-Dec-13 9:00am    
Great answer, +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900