+ 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

26th Nov 2022, 6:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Respostas
+ 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
26th Nov 2022, 10:14 AM
Ipang
+ 1
Thats correct Ipang but why so ? Like advance, why prev dont have void return type ?
26th Nov 2022, 3:45 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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 : )
26th Nov 2022, 4:10 PM
Ipang