Search code examples
c++iteratorstdstdlist

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?


Solution

  • 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.