+ 1
Why prev and advance have different implementation
Hi Refer code below : I know now that advance modifies input argument and prev need to assign return type. Perfectly fine to follow this syntax but curious to know logic behind the same. Whats the reason of not modidying input argument of prev and giving output as return type. https://code.sololearn.com/cu9O2i7WbaJX/?ref=app
3 Réponses
+ 1
Hello Ketan,
From the <iterator> page in cppreference I see that advance() has a void return type, while prev() and next() returns iterator type. prev() and next() came with C++11
https://en.cppreference.com/w/cpp/iterator/advance
https://en.cppreference.com/w/cpp/iterator/prev
https://en.cppreference.com/w/cpp/iterator/next
+ 1
Thats correct Ipang but why so ? Like advance, why prev dont have void return type ?
0
Honestly I mever thought of it that far : D
But perhaps returning an iterator was the only way because prev() and next() both accepts the iterator argument by value.
Whereas advance() accepts the iterator argument by reference, which allows advance() to directly modify the iterator argument, and thus doesn't need to return anything.
But for actual reason why, I guess we'll have to ask the language developers : )