I have a question about lists. I have an empty list A which I populate with integers according to an external computation. When the process finds a new integer which is not in the list A, it adds it in A and, in a second list (B), it creates at the same index an integer for the previous integer's occurrence. So it starts with 0 and increment B[index]++ when process finds an integer which exists in A. At the end, lists can be like this :
List A = {4, 2, 8, 6}
List B = {2, 3, 4, 1}
So it means that the process found ;
2 times an integer 4,
3 times an integer 2,
4 times an integer 8
and 1 time an integer 6.
Before the output, I want to sort integers in the list A, but (and this is my question) I would like that B could imitateA because of I sorted A, so B is no more exact according to new A values' indexes.
List A = {2, 4, 6, 8} <- sorted list List B = {2, 3, 4, 1} <- how to sort it according to new A
Do you have an idea how I should develop this function ? Thank you for your help. I wish you the best ++
Thank you mbozzi for you explanation. It seems really interesting and more efficient than my previous code. However I tested your code and it does not work as expected. There is something wrong with the occurrences. The final output is wrong ++
PS : Quickly I found a way, but I don't know if my fix is the better way (x.count + 1) :
for (value_count x : xs) std::cout << x.value << " occurred " << x.count + 1<< " time(s).\n";
Alternatively you could use std::pair instead of defining a custom struct
I chose not to use pair because the member names value and count leave less to be inferred from context than first and second. Additionally, value_count is much less complicated than std::pair - its behavior holds no surprises.
An argument in pair<int, int>'s favor is that it supports all six comparison operators out of the box, and that its associated operator< predictably implements a strong total order.
IMO, std::vector almost always is preferred over a linked-list (std::list) anyway.
Phillip Johnston wrote:
General Rules of Thumb
There are some general rules of thumb that will guide you through most situations:
- Use sequential containers when you need to access elements by position
- Use std:vector as your default sequential container
- If you add or remove elements frequently at both the front and back of a container, use std::deque
- Use a std::list (not std::deque) iff you need to insert/remove elements in the middle of the sequence
- Do not use std::list if you need random access to objects
OP did say "list", though it might not have been deliberate.
As mentioned above, one should probably use std::vector as the default "list" (sequence container) implementation and only resort to std::deque or std::list when actually required for specific reason.