c++11 - Why auto iteration in C++ cannot perform operation on the element it points to? -


i spent ~2 on trying figure out bug introduced code due usage of auto iteration containers. started using couple of days ago, without doing background check, because found easier write.

i have following map: std::map<int, vectorlist>, vectorlist typedef std::vector<double> vectorlist.

i wanted perform .clear() operation on std::vector<double> of vectorlist.

i tried following:

std::map<int, vectorlist> map; for(auto elem : map) {     elem.second.clear();  } 

and did not work. clear operation not being performed on vectorlist. when performing .empty() check on it, return true.

then went approach:

for(std::map<int, vectorlist>::iterator elem = map.begin(); elem != map.end(); ++elem) {     elem->second.clear(); } 

and worked expected.


question:

why auto iteration not perform .clear() operation expected? can achieved auto iteration @ all?

because elem created by value. if want modify value in loop loop using references:

for(auto& elem : map) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -