Why use dereference operator on iterators?
Nov 21, 2012 at 7:24pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
vector<string>::iterator myIter;
vector<string>::const_iterator iter;
vector<string>weap(3);
string userInput;
cout << "Enter 3 weapons: " << endl;
cin >> weap[0];
cin >> weap[1];
cin >> weap[2];
cout << "Your weapons: " << endl;
for (iter = weap.begin(); iter < weap.end(); ++iter)
{
cout << *iter << endl;
}
(I've not used the other parts of the program to save space e.g #include <iostream>)
The book I'm using doesn't go into enough detail to why we use the dereference operator (*) when outputting operators. So if someone kind enough could explain to me why we need to use them.
Nov 21, 2012 at 7:30pm UTC
iterators are analogous to pointers. You dereference them to get at the pointed-to object.
Nov 21, 2012 at 7:32pm UTC
cire wrote:iterators are analogous to pointers. You dereference them to get at the pointed-to object.
+1.
Iterators, for safety, performance and simpleness store pointers, not objects.
Nov 21, 2012 at 7:32pm UTC
Ah, that's more clear. Thanks :D
Topic archived. No new replies allowed.