I recently became interested in the question of which of the various methods of passage through the collection are the most effective and productive, and in general when and in what situation is it better to use one or another method? We take std :: vector as a basis.
... std::vector<std::string> cities; std::string rostov = "Ростов"; std::string omsk = "Омск"; cities.push_back(rostov); cities.push_back(omsk); ... 1 Method - the most primitive
for (size_t i = 0; i < cities.size(); i++) { std::cout << cities.at(i) << std::endl; } 2 Method - range for
for (const auto& city : cities) { std::cout << city << std::endl; } 3 Method - iterators
for (auto it = cities.begin(); it != cities.end(); it++) { std::cout << *it << std::endl; } 4 Method - lambda
std::for_each(cities.begin(), cities.end(), [] (std::string &city) { std::cout << city << std::endl; }); For example, a vector with two elements was given, I would like to talk about a large volume of containers. And I would also like to hear about which of the options is better and why, if the body of the cycle does not just output its elements (one could do so std :: copy (...)), but perform some kind of calculation over the elements . Thank you in advance...