Sign in to follow this  

Why do I have to dereference my vector iterator? What does it mean?

This topic is 2006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote this code to test:

[code]

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

using namespace std;

struct Person {
string name;
int age;
};

int main()
{
// Populate some people and put them into a vector.

Person p1, p2, p3;
vector<Person> ListOfPeople;

p1.name = "Richard";
p2.name = "Mike";
p3.name = "Cheryl";

p1.age = 12;
p2.age = 13;
p3.age = 14;

ListOfPeople.push_back(p1);
ListOfPeople.push_back(p2);
ListOfPeople.push_back(p3);

vector<Person>::const_iterator citer = ListOfPeople.begin();

while ( citer != ListOfPeople.end() )
{
cout << (*citer).name << " is " << citer->age << " years old." << endl;
++citer;
}

return 0;
}

[/code]

A clean version can be found at [url="http://codepad.org/hAumqzNB"]http://codepad.org/hAumqzNB[/url] with the associated output.

I know this has to do with pointers. I taught myself C++ once before using online tutorials but this book hasnt gone into pointers though it has covered references. This is one thing I never really understood about the language.

My questions:

Why do I have to deference this to make it work?
What exactly does dereferencing normally do?
Why on other sites do they say pointers are a type of iterator?
Lastly, object->member just a synonym of (*object).member and nothing more?

Edit: the book says derefencing gives an lvalue. Which i understand is a nontemporary object. So when you use an iterator does it act as a reference to the vector? Because I know you need lvalues to access data because of scoping issues. So does this essentially make a copy of the object in memory for the purpose of printing to the screen? aka an lvalue?

Sorry if I don't understand properly, its why im here :/

Thanks in advance for all your help!

Senjai Edited by Josh Petrie

Share this post


Link to post
Share on other sites
[quote name='Katreyo' timestamp='1339696050' post='4949213']Why do I have to deference this to make it work?[/quote]
Because an iterator is a pointer built for you by the vector class.

[quote name='Katreyo']What exactly does dereferencing normally do?[/quote]
As a pointer is just a memory address itself, it asks memory for the value at the address which the pointer is "pointing".

[quote name='Katreyo']Why on other sites do they say pointers are a type of iterator?[/quote]
Because pointer arithmetic lets you navigate memory sequentially with the same pointer primitive, and dereference different addresses, just like an iterator (because, let's be honest, an iterator is just a pointer for your collection)

[quote name='Katreyo']Lastly, object->member just a synonym of (*object).member and nothing more?[/quote]
That's correct.

Share this post


Link to post
Share on other sites
Thank you BCullis, that cleared everything up. In terms of pointers as iterators I guess thats something I'll have to learn later. Really appreciate your help.

Share this post


Link to post
Share on other sites

This topic is 2006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this