How do I get a const_iterator using auto?

前端 未结 8 1779
佛祖请我去吃肉
佛祖请我去吃肉 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:05

    Sorry, but I just think the best suggestion is not using auto at all, since you want to perform a (implicitly valid) type conversion. auto is meant for deducing the exact type, which is not what you want here.

    Just write it this way:

    std::map::const_iterator city_it = usa.find("New York");
    

    As correctly pointed out by MooingDuck, using type aliases can improve the readability and maintainability of your code:

    typedef std::map my_map;
    my_map::const_iterator city_it = usa.find("New York");
    

提交回复
热议问题