Jump to content
  • Advertisement
Sign in to follow this  
Katreyo

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

This topic is 2222 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:



#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;
}



A clean version can be found at http://codepad.org/hAumqzNB 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
Advertisement
Why do I have to deference this to make it work?

Because an iterator is a pointer built for you by the vector class.


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?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!