Hi guys. I've been reading about ways of finding duplicates in a container, but I still lost of how to solve my problem which is how can I count the different number of duplicates in a vector<int> container.
Let's imagine I have the following container: { 1, 1, 1, 3, 3, 3, 3, 8, 8 ... } How can I count and if possible, put the count result in a variable? for example a = 3 (because of three 1's), b = 4, c = 2.
What exactly you want to do? Maintaing a relation like "this sequence contains 3 1's, 4 3's and 2 8's"? Something else?
Is your sequence always sorted? Can you change your sequence?
Yes, the sequence is always sorted. I need to know how many of each duplicates exist in a container, thats all, because I'll use this information (the quantity of each number) after. I don't need to change anything in the container.
What if I have, 500 different ints? e.first and e.second will show all of then? or just two of then? And how can I access the information of how many times each number exists? (i.e.: Element 1 encounter 3 times)
This is a range-for loop.
On each iteration e is assigned a value from container.
That value ib both cases is std::pair: a structure which contains two values named first and second.
In both cases this pair denotes a frequency entry: first member says which number and second says how many times it is encountered.
So, this loop will show all possible unique elements and their frequency.
I have a vector based vector. I created this vector using the .push_back() function. So, I have a 1D array vector (with ints), And I can use the array deference style to access its elements, right? Now, I need to know how many times each element is repeated.
Look again at MiiNiPaa's "Using vectors and algorithms" program.
It does use C++11 vector::emplace_back(), but you can use C++98 vector::push_back().
With both those methods I do recommend adding frequency.reserve( unique.size() );
before the counting-loop. That avoids unnecessary reallocations.