Domanda

If you're looping through a container as such:

typedef std::vector<std::unique_ptr<BaseClass>> Container;
Container container;

for(Container::const_iterator element = container.begin(); element != container.end(); element++)
{
    //Read through values
}

And instead of using the typedef you decide to use auto:

std::vector<std::unique_ptr<BaseClass>> container;

for(auto element = container.begin(); element != container.end(); element++)
{
    //Read through values
}

Assuming you don't alter the values, does the auto keyword use a const iterator over a non const one?

This question is curiosity more than anything, the only reason I can see this being an applicable question in a real life scenario would be if you needed to communicate that you weren't to alter values to another person working on the code.

È stato utile?

Soluzione

1) Use cbegin and cend to be explicit about using const iterator.

2) begin() and end() return const_iterator when method is declared as const

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top