Please explain this use of star operator

Hello there, I have this vector of pointers to strings, let me illustrate...

1
2
3
4
5
6
7
8
9
10
11
int main() {
	
	vector<string*> strvec;
	string *str = new string("string content");
	strvec.push_back(str);
	vector<string*>::iterator i = strvec.begin();
        //this line 
	cout << *(*i);
	delete *i;
	return 0;
}


As you can see I use an iterator to point to the first position of the vector, now what I would like to know is why I must use the * operator twice as in:
cout << *(*i);
to yield the object to which the iterator points ?

Thanks in advance,

Jose
That's because iterators pretend to be pointers. The inner one converts vector<string*>::iterator to string* and the outer one, gets the string.
edit: typo
Last edited on
I think I need more explaining....
I didn't understand this part you just mentioned:
The inner one converts vector<string*>::iterator it string*
1
2
3
i; //an iterator to vector<string*> 
*i; // a string pointer
**i; //a string 

Why are you using string pointers instead of string objects?
1
2
3
4
5
vector<string> strvec;
strvec.push_back("string content");
vector<string>::iterator i = strvec.begin();
cout << *i; //you can do strvec[0] too
//no need to delete 
Good. I find this last explanation clear enough. I think I must trust the double star operator to yield the value out of a pointer but just in this case that the iterator points to a pointer. were it not a pointer I think the double star operator would not be required. I hope this is correct.
Topic archived. No new replies allowed.