Use the auto keyword in C++ STL

后端 未结 6 1691
野趣味
野趣味 2020-12-23 00:10

I have seen code which use vector,

vectors;
s.push_back(11);
s.push_back(22);
s.push_back(33);
s.push_back(55);
for (vector::iterator i         


        
6条回答
  •  独厮守ぢ
    2020-12-23 00:29

    This is new item in the language which I think we are going to be struggling with for years to come. The 'auto' of the start presents not only readability problem , from now on when you encounter it you will have to spend considerable time trying to figure out wtf it is(just like the time that intern named all variables xyz :)), but you also will spend considerable time cleaning after easily excitable programmers , like the once who replied before me. Example from above , I can bet $1000 , will be written "for (auto it : s)", not "for (auto& it : s)", as a result invoking move semantics where you list expecting it, modifying your collection underneath .

    Another example of the problem is your question itself. You clearly don't know much about stl iterators and you trying to overcome that gap through usage of the magic of 'auto', as a result you create the code that might be problematic later on

提交回复
热议问题