Iterators

Hi sorry If I'm been asking a lot of questions today I'm trying to get a lot covered haha

One thing that is just wrecking my brain is the concept of iterators I've been watching tutorials and reading up on what they are and all I get is people mentioning containers(what is a container :s) and the STL which I have no clue what they are talking about.

so in laymans terms can anyone explain what an iterator is? what does it do and how is it used?

Thanks


Last edited on
An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (with at least the increment (++) and dereference (*) operators).

http://www.cplusplus.com/reference/iterator/

A container is a type that "contains" other elements.. i.e. a vector, an array, a list, a set, etc.
Containers implement iterators for iterating through the elements of the container. For instance, take a std::vector. std::vector has a member type vector<sometype>::iterator, which can be used to iterate through the vector. vector::begin() returns an iterator to the first item in the vector. Incrementing that iterator, then points to the next element in the vector.
http://www.cplusplus.com/reference/vector/vector/
http://www.cplusplus.com/reference/vector/vector/begin/

and the STL which I have no clue what they are talking about.

STL is the Standard Template Library. That's pretty much everything in the std namespace. It includes vectors, arrays, lists, sets, maps, etc.
http://www.cplusplus.com/reference/stl/
STL is the Standard Template Library. That's pretty much everything in the std namespace.


https://en.wikipedia.org/wiki/Standard_Template_Library

The STL existed apart and separate from anything in the std namespace. The analogs in the standard library haven't necessarily kept the same identifiers.

To the OP: The abbreviation is commonly misused to refer to the standard library, but you should be aware of the distinction.
thanks for all the input guys,

just wondering why does the function reverse() only accept iterators why not integers whats the difference? how would a function even have a return type of an iterator in the first place?


also how come this won't work reverse(exampleVector.at(0),exampleVector.at(4)); yet this will work reverse(exampleVector.begin(),exampleVector.end()); is exampleVector(0) not the same thing as exampleVector.begin(); they both should have the same value right?? that's so confusing

Thanks
just wondering why does the function reverse() only accept iterators why not integers whats the difference?


An integer is a number. A single number. How would reverse(3,17) work? I've got a vector with ten numbers in it. The first number is 3, and the last number is 17. Now you tell me what the reverse of that vector is.

just wondering why does the function reverse() only accept iterators why not integers whats the difference? how would a function even have a return type of an iterator in the first place?

iterators are objects that know how to get from one element to the next. reverse doesn't need to know this, so reverse depends on the iterators to do that. Otherwise, reverse would have to know how to get from one element to the next in an array, in a list, in a deque, in a vector... that would very much complicate the implementation of reverse and make it both less maintainable and less flexible.

also how come this won't work reverse(exampleVector.at(0),exampleVector.at(4));
at returns a reference to an element of the vector, not an iterator.


yet this will work reverse(exampleVector.begin(),exampleVector.end());
begin and end return iterators.

If you wanted to specifically reverse only the first five elements:
std::reverse(exampleVector.begin(), exampleVector.begin()+5); Assuming, of course, there are at least five elements.


is exampleVector.at(0) not the same thing as exampleVector.begin(); they both should have the same value right??

No, and no they shouldn't. On the other hand, exampleVector.at(0) and *exampleVector.begin() will have the same value. You must dereference an iterator to get at the element to which it refers.



I think I kind of understand,so in a way an iterator is pointing to elements in a container like a Vector but not their actual value? so VectorExample.begin() is pointing to the memory location of the first element?

and whats the actual difference between exampleVector.at(0) and exampleVector.begin();
Last edited on
also on a side note relating to iterators

how come this code will work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

int main()
{
  map<string,int> one;
  map<string,int>:: iterator it;

  one["adam"] = 23;
  one["jack"] = 24;
  one["trey"] = 21;

  for(it = one.begin(); it != one.end(); it++){

        cout << it-> second << endl;
  }
}


but not this,here I am just derefrencing the iterator to get it's value yet it won't work??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

int main()
{
  map<string,int> one;
  map<string,int>:: iterator it;

  one["adam"] = 23;
  one["jack"] = 24;
  one["trey"] = 21;

  for(it = one.begin(); it != one.end(); it++){

        cout << *it << endl;
  }
}


or this here I am just saying one less than one.end();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

int main()
{
  map<string,int> one;
  map<string,int>:: iterator it;

  one["adam"] = 23;
  one["jack"] = 24;
  one["trey"] = 21;

  for(it = one.begin(); it != one.end() -1; it++){

        cout << it->second << endl;
  }
}

Last edited on
yet it won't work??
or this here I am just saying one less than one.end();


I'm sure we already covered the art of asking questions today. "it won't work??" isn't a question. It's you being too lazy to ask your question and hoping that we'll work out your question for you, and then answer it.



What kind of object is *it ?
It's a pair<string, int>
What did you expect to happen when you passed a pair<string,int> to cout << ?


or just maybe I genuinely don't know why it won't work after researching it and am asking to see if anyone can give me pointers.

and yes I know it's a pair but I thought it might print out the first variable when I derefference it
Last edited on
@Moschops: Calm down, he's trying to understand a difficult concept.


@adam2016: If you have a vector of ints, the vector iterator will iterate over ints. Thus, dereferencing a vector iterator will give you the int value. Now a map is made of pairs. In other words, every element of a map is a pair<T1, T2> type. In your case, the map is made up of pair<string, int> elements. Map iterators will iterator over pairs, and thus when you dereference the map iterator, you get pair<string, int> element.

Every pair has a first and second member. In order to access the int part, you need to first dereference the iterator to get the pair, then access the second member of the pair.

1
2
3
4
it // iterator 'pointing' to a pair<string, int> element
*it // gives you pair<string, int>
(*it).second // gives you the int value
it->second // fancy way of writing the previous line.  


This is why the first program works (successfully accessing the pair, then getting the int value in that pair), while the second one fails (as its trying to cout a pair<string, int> which does not make sense).

As for the third program, maps are not random access containers like vectors, thus they do no support operator+ or operator-. You can, however, use the increment or decrement operators.
Last edited on
that makes a lot of sense Arslan,I understand that completely much appreciated =)
You may want to read this tutorial: https://cal-linux.com/tutorials/STL.html
I'm understanding the concept a lot more that iterators are a way to traverse a container but what is the differences between using iterators and using indexes?

from my understanding you would use iterators if you wanted to use them in the algorithm class functions like sort(),reverse() which only accept iterators am I right? and when would you use iterators as opposed to indexes(other than what I said before)?


for example

indexes

1
2
3
4
5
6
7
8
9
10
11
12
13

vector<int> one;
    vector<int>:: iterator it;

    one.push_back(60);
    one.push_back(30);
    one.push_back(50);

    for(int i = 0; i < one.size();i++){

          cout << one.at(i) << endl;
    }



and iterators

1
2
3
4
5
6
7
8
9
10
11
12
13
14

vector<int> one;
    vector<int>:: iterator it;

    one.push_back(60);
    one.push_back(30);
    one.push_back(50);

    for(it = one.begin(); it != one.end();it++){

         cout << *it << endl;

    }
Lots of functions don't take an index. They do take an iterator.

http://en.cppreference.com/w/cpp/algorithm
I'm understanding the concept a lot more that iterators are a way to traverse a container but what is the differences between using iterators and using indexes?

Some containers don't lend themselves to indexing. i.e. a linked-list, a set, a map.
thanks guys,I'm slowly getting the concept.
The idea is to have one consistent way to traverse ANY container. Not all containers support indexing, thus iterators are used as a wrap-around so they can work with any container.
Topic archived. No new replies allowed.