7

I have just started learning vectors and iterators. I can't understand 2 things. Why can I change the constant iterator and what is the role of the "*"?

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    vector<string> inventory;
    inventory.push_back("inventory1");
    inventory.push_back("inventory2");
    inventory.push_back("inventory3");
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;
    cout << "Your items:\n";
    for (iter = inventory.begin(); iter != inventory.end(); iter++)
    {
        cout << *iter << endl;
    }
9
  • Any iterator can change value, unless it is declared const. A const_iterator refers to the object being "pointed to" as const. The * is to retrieve the value the iterator "points" to. Commented Sep 21, 2015 at 13:39
  • 2
    const_iterator doesn't mean that the iterator is a constant, but that you cannot change the item it refers to. Commented Sep 21, 2015 at 13:42
  • That code is correct. I just cant stand why "iter", which is const_iterator, can change his value and i cant understand what you mean about the"*" Commented Sep 21, 2015 at 13:43
  • I guess i should read more to understand what are you trying to say. Commented Sep 21, 2015 at 13:45
  • 1
    @owacoder - Don't confuse the poor guy with pointers. They are hard! And to be strict, a pointer is a kind of random access iterator. Not the other way round. :-) Commented Sep 21, 2015 at 13:49

2 Answers 2

10

When you do iter = inventory.begin() it makes iter refer to the first string in your vector. iter++ moves it to refer to the next string.

In the output you use *iter as way to access the string that iter refers to. In the first output that will be inventory1.

The slight confusion about the constness is that

vector<string>::const_iterator   iter;

is an iterator that refers to things that are constant, while

const vector<string>::iterator   iter;

would make the iterator itself constant, but allow you to modify the object it refers to.

Sign up to request clarification or add additional context in comments.

2 Comments

So, with * i let the iterator to output it ? Before that it only refers to but with * i give him access ?
But what is the role here ?:'myIterator = inventory.begin(); *myIterator = "inventory4"; cout << "\nYour items:\n"; for (iter = inventory.begin(); iter != inventory.end(); iter++) { cout << *iter << endl; } of the * ?
5

Why can I change the constant iterator

You have a non-const iterator of type vector<string>::const_iterator. You can change it because it is non-const. Being a const_iterator means that dereferencing it returns a const reference to an object. Which means that the referenced object cannot be modified.

The iterator is non-const because the type vector<string>::const_iterator does not have a const qualifier.

what is the role of the "*"

It is the dereference operator. It returns the object that the iterator is currently pinting at. More exactly, it returns a reference to the object. Another name for it is indirection operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.