Hello everyone,
I have a small question. I have two vectors as follows:
A=[3 0 0 7]
B=[5 3 8 9]
I need to find the index of the smallest values of A which are 0,1 which correspond to the value 0.
Then, I need to choose the index amoung these indexes which corresponds to the smallest value in B ie, I need to choose index 0 between 0 and 1, which corresponds to smallest value ,3, in vector B. The problem I face, I do not want to use push_back to save the indexes of A since it is costly. How can I achieve this task in an easiest and least costly way ?
Thank you so much
The smallest value in A is apparently 2. The value 2 appears at indices 1 and 3.
B=[5 4 8 9]
Indices 1 and 3 in B contain values 4 and 9. The smallest of them is 4.
The push_back(), specifically vector::push_back() may be costly if vector has to reallocate. It is possible to preallocate, which ensures that push_back will not reallocate. Hence that cost is not so real.
However,
auto aMin = *std::min_element( begin(A), end(A) );
FOR each index i
IF Ai == aMin
THEN consider Bi for bMin
Let's clarify some things: You're asking for the most efficient way to find the smallest corresponding value between two vectors. Is it the value that you need or do you specifically need the index of that smallest value?
I'm assuming both the vectors may be any length in this problem.
How do you feel about sorting the vectors? Or does the index of the values have to remain unchanged?