when should i use iterators?

Started by
6 comments, last by Onemind 19 years, 6 months ago
I was wondering when is it right to use iterators .
-----------------------------------Panic and anxiety Disorder HQ
Advertisement
you would use an iterator when you wanted to uhhh, iterate ? hehe, seriously though, yeah, you would use them to loop through something like a std::list or std::map and such.

EDIT: im not sure i really understood your question....
FTA, my 2D futuristic action MMORPG
You can also use iterators to store a location in the std::list, std::map, etc. Note tho that this is for temporary or short-length situations at best, since changing the contents or ordering of the container could change the value of the iterator

Drew Sikora
Executive Producer
GameDev.net

That is a secksy avatar. Kingdom Hearts, right? I wish I still had a PS2 just for that game. But then...that costs money.

Anyway, iterators are used to loop through things. Read this question half an hour ago and I decided to just leave it alone, but I, too, want you to elaborate upon the quEstIOn thaT is aTa HANDA, so thata I can'a HELPA you witha your pro-ba-leMA!

[/rant]
Things change.
Use iterators for two three things:

1. Where you used to use an index variable when looping through an array.

2. Where you used to use an index variable or pointer to refer to a specific element in an array.

3. Wacky C++ iterator stuff like ostream_iterator.
another place you would want to user iterators is when you want to use std:: algorithms... alot of them take iterators to operate on... same thing goes with member functions of std containers, things like .erase()
FTA, my 2D futuristic action MMORPG
Quote:Original post by Boku San
That is a secksy avatar. Kingdom Hearts, right? I wish I still had a PS2 just for that game. But then...that costs money.

Anyway, iterators are used to loop through things. Read this question half an hour ago and I decided to just leave it alone, but I, too, want you to elaborate upon the quEstIOn thaT is aTa HANDA, so thata I can'a HELPA you witha your pro-ba-leMA!

[/rant]

its cloud from ff7 and thnks guys
-----------------------------------Panic and anxiety Disorder HQ
Iterators were created in C++ to simplify data retrival. I generally use them whenever I want to loop thru every element in a vector, queue, ect. That question is kind of like asking "when should I use a while loop" - it depends.

This topic is closed to new replies.

Advertisement