How do I get a const_iterator using auto?

前端 未结 8 1798
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 08:18

First question: is it possible to \"force\" a const_iterator using auto? For example:

map usa         


        
8条回答
  •  生来不讨喜
    2020-12-13 09:07

    A clean solution is to work with a const reference to the otherwise modifiable map:

    const auto &const_usa = usa;
    auto city_it = const_usa.find("New York");
    

    This will make sure you can't modify const_usa, and will use const iterators.

提交回复
热议问题