The use of Iterators instead of Indexing

I was reading the Wikipedia article on Iterators and found this:


An iterator may allow the container
object to be modified without invalidating
the iterator. For instance, once an iterator
has advanced beyond the first element it
may be possible to insert additional
elements into the beginning of the
container with predictable results. With
indexing this is problematic since the index
numbers must change.


I can't recall an implementation of the above statement in C++11. Specifically, I can't recall the process of "gaining predictable results" when inserting elements randomly using iterators in contrast to the use of subscript operator. Can anyone show me an example? Thanks!
closed account (Dy7SLyTq)
does it mean something like this?
1
2
3
4
5
6
7
vector<int> Container(1);

for(auto &Counter : Counter)
{
	Counter.resize(1);
	Counter = 1;
}
If you have std::list, inserting anywhere in the container does not invalidate iterators, so it's still pointing at what it used to point to.. granted, there is no indexed access in std::list, so the quote seems wrong.
@DTSCode: I don't understand your code. You have used a range for loop which uses a reference variable to iterate through itself?

@Cubbi: Consider using std::array. Is there any reason (except compatibility) why I should prefer using Iterators instead of Indexing or the use of the Subscript operator?
Iterators I like pointers to the objects in your container, and if you know how valuable pointers are to use you prefer Iterators. Below is a script that shows some of the useful things and Iterator can do.
Notice in Line 47 how you can modify were you erase or put something by doing simple arithmetic, another version of the line could have been.

inventory.erase((inventory.end() - 1));

also notice how you can change the value of the object the iterator is referencing by using the dereference operator (*), but you dont change the value of the iterator itself. (Line 29).

you can do all sorts of things. any more questions on iterator just ask.



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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Hero's Inventory 3.0
// Demonstrates iterators

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> inventory;
    //add item to the vector of string using push_back.
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");

    //Creation of an interator
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;

    cout << "Your items:\n";
    //loops through the different items in the vector.
    for (iter = inventory.begin(); iter != inventory.end(); ++iter)
        cout << *iter << endl;

    cout << "\nYou trade your sword for a battle axe.";
    myIterator = inventory.begin(); //Set iterator's position or spot
    *myIterator = "battle axe"; //set the value that the iterator references to "Battle Axe";
    cout << "\nYour items:\n";
    for (iter = inventory.begin(); iter != inventory.end(); ++iter) // Loop throught Items againg.
        cout << *iter << endl;

    cout << "\nThe item name '" << *myIterator << "' has ";
    cout << (*myIterator).size() << " letters in it.\n";

    cout << "\nThe item name '" << *myIterator << "' has ";
    cout << myIterator->size() << " letters in it.\n"; //Get the size of the string that the iterator references.

    cout << "\nYou recover a crossbow from a slain enemy.";
    inventory.insert(inventory.begin(), "crossbow"); //Add a item in the vector using the insert function.
    cout << "\nYour items:\n";
    for (iter = inventory.begin(); iter != inventory.end(); ++iter) // Loop throught Items againg.
        cout << *iter << endl;

    cout << "\nYour armor is destroyed in a fierce battle.";
    inventory.erase((inventory.begin() + 2)); // Notice the parameter in erace function is not just inventory.begin(), but inventory.begin() + 2.
    cout << "\nYour items:\n";
    for (iter = inventory.begin(); iter != inventory.end(); ++iter)
        cout << *iter << endl;

 return 0;
}
Last edited on
@Hertz: I wasn't asking what Iterators are and how they work. I was asking the reasons (except compatibility) why I should prefer using Iterators instead of the Subscript Operator if I use std::array.
IMO the main strength of iterators is it brings you closer to container independence. There are many reasons why you may wish to change to another container type later on. Code that operates on that container using indexes (the subscript operator) cannot be changed as easily. (insertion / deletion of data in the container invalidating the indexes being the main problem).

If your container will never ever ever change - then.... never mind.
Topic archived. No new replies allowed.