Reputation: 688
I have a problem with iterator. Could you explain me why this code:
vector<vector<int> >::iterator it = v.begin();
for(; it < v.end(); it++)
{
vector<int> var = *it;
sort(var.begin(), var.end() );
}
is ok and with this code:
vector<vector<int> >::iterator it = v.begin();
for(; it < v.end(); it++)
{
sort(*it.begin(), *it.end() );
}
is wrong? Compiler said that *it has no member begin, but I don;t know why.
Upvotes: 2
Views: 2874
Reputation: 105886
The operator "." binds stronger than the operator "*". Try
vector<vector<int> >::iterator it = v.begin();
for(; it < v.end(); it++){
sort(it->begin(), it->end() );
}
instead.
Your code works like this:
vector<vector<int> >::iterator it = v.begin();
for(; it < v.end(); it++)
{
sort(*(it.begin()), *(it.end()) );
}
and it
simply has no member begin
as it
is a simple iterator.
Upvotes: 4
Reputation: 355069
Operator precedence.
*it.begin()
is the same as *(it.begin())
. You need (*it).begin()
(or the equivalent expression, it->begin()
).
That is, you need to "call the member function begin()
of the object pointed to by it
," not "deference the result of calling the member function begin()
on it
" (it
has no member function begin()
, which is why the compiler gives you the error message that you get).
Upvotes: 11