Vectors Quize Q No 6

How swap function work in O(1) time

hello @asif_h when we are swapping any two numbers we know what two values we have to exchange.
we don’t have to run for loops for this:
just by indexing we can get the element in constant time in that way only we are swapping two values in the constant time.
because we already have the indexes of values wich we have to swap.

But in this question they swap two vector
Here written v1.swap(V2).so in order to swap all elements of vector thn our time complexity becomes O(n)

hey @asif_h they are not iteratinf across every elemnt of the vectors and swapping the elements.
but they are just swapping the pointer to the vectors and when they will be swapped both the pointers will iterate to other vectors and this is done in constant time.

Ok thank you .Now it is cleared

@asif_h if you feel that your doubt is cleared you can now mark this doubt as resolved.
Happy Learning !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.