Megidd
Megidd

Reputation: 7952

Understanding size() method of std::list iterator

    std::list<std::vector<unsigned long> >::const_iterator it;

// ...

        std::vector<unsigned long> non_mf;
        non_mf.reserve(it->size());

What's the meaning of it->size() above? Size of iterator, what does it mean?

Upvotes: 0

Views: 410

Answers (2)

eerorika
eerorika

Reputation: 238351

You aren't calling a member function if the iterator because you don't use the operator ..

Operator -> is an indirecting member access operator. It indirects through the left hand operand and accesses the member of the indirection result. When you indirect through an iterator, you get the object that the iterator points to, so what you're doing is calling a member function of the pointed object which in this case is a vector.

You could get the same result using the unary indirection operator * and then the non-indirecting member access operator .:

(*(it)).size()

This isn't quite as readable nor as pretty as using operator ->, which should explain why the latter operator exists in the language.

Upvotes: 2

yurand
yurand

Reputation: 81

When deferenced an iterator refers to the methods and fields of the object pointed. In your case it is an iterator of a list of vectors, so when deferenced it refers to one of the vectors in the list, and so you are asking the size of that vector.

Upvotes: 3

Related Questions