I have a doubt regarding next( ) funtion

Could you tell me in what all data structures i need to use next( ) fn to access the next iterator.

@laksh.rustagi12 you can use next() on any iterable container.

for sets is next(it) equal to it++ ?

@laksh.rustagi12 yes if you dont specify the second argument in next its taken 1 by default so it’ll point to next element, ie it++
Advantage of next is that you can hop multiple elements too at a time, so instead of having to do it++ 4 times you can simply write next(it, 4)

If the container has a bidirectional iterator then you can have a negative value of n also and move backwards.


change next to ++it in line number 50, it’s giving different answer, so it means ++it and next(it) are not the same??

@laksh.rustagi12 next only gives you a copy, it doesnt change the original iterator

How do you remember such details about a function that’s not much used, i mean have thoroughly read gfg when you were learning dsa?

oh okay, thankyou … … …

@laksh.rustagi12 no one can remember all these tiny details, I usually refer to the docs to be sure.
https://www.cplusplus.com/reference/iterator/next/

1 Like

@laksh.rustagi12 please mark the doubt as resolved if you dont have any more questions regarding this


here’s how you can use next() for loops

1 Like