C++ 11 Range Based Loop
Repost from https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const
If you don't want to change the items as well as want to avoid making copies, then
auto const & is the correct choice:for (auto const &x : vec)
Here is recap:
- Choose
auto xwhen you want to work with copies. - Choose
auto &xwhen you want to work with original items and may modify them. - Choose
auto const &xwhen you want to work with original items and will not modify them.
Comments
Post a Comment