You miss the point as to why we use containers rather than do manual memory management, except of course when we are implementing the said containers or specialised data structures/smart pointers for which existing containers or existing smart pointers might not be suitable as underlying implementation. I happened to link to this article just now in another thread, so I might as well repeat it here... read Stroustrup's answer to the FAQ: How do I deal with memory leaks?Originally Posted by userxbw
A std::vector<int> is the typical replacement for a dynamic array implemented using new[]/delete[]. Granted, it does more than what is needed in this case, and if one really wanted there are alternatives that do manage their own memory, but generally it is the most typical solution to reach for here.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
try to think like the instructor, looks like he is trying to just teach shorting here not what you're trying to show me. if he uses a vector it totally eliminates the need to use anyone of the three types of sorting the array, just call to sort, start,end then return it, then print it. the exercise has been made totally useless. teachers do not jump in head first then work their way backwards to the basics.
Last edited by userxbw; 10-02-2017 at 05:44 PM.
That was my point when I suggested that it was enough to use the original solution involving fixed size arrays, not dynamic memory allocation. Once you want to touch dynamic memory allocation, then either it should have already been taught after the use of containers, or you should suggest the use of an appropriate container instead. The use of dynamic memory allocation is not the basics, hence once you suggest it, it is perfectly valid for me to suggest the use of std::vector<int> instead, since "he is trying to just teach shorting here not what you're trying to show me".Originally Posted by userxbw
I recommend that you read Stroustrup's essay on Learning Standard C++ as a New Language (PDF) to understand why this is so.
That is not true. The use of std::vector does not eliminate the need to combine the input and sort it. If it could do that, then so could the use of new[]/delete[], or the use of fixed size arrays.Originally Posted by userxbw
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)