Answer the question
In order to leave comments, you need to log in
What is the advantage of functions over container methods?
In the latest versions of C++ (11, 14, 17), functions similar to some methods of standard containers appeared in the std namespace. For example std::size, std::begin, std::end.
Why is this needed? After all, every container used to have .size() and .begin()
methods. Why are such "global" functions better than methods?
std::vector someData{1, 2, 3};
// Чем это
std::size(someData);
// Лучше этого?
someData.size();
Answer the question
In order to leave comments, you need to log in
Well, right away we get the opportunity to call std::size() also for C-arrays, in addition to standard containers.
Also, for non-standard containers, which do not have access to the code, you can always add free functions begin(), end(), size() , unlike methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question