We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Instead of using a for loop paired with push_back() (which can result in allocations that could otherwise be avoided), use the explicit constructor, std::vector<T>(int) (requiring direct initialisation via a parenthesised list) which constructs a std::vector of a given size:
intmain(){intsize{};std::cin>>size;// Constructs a 'std::vector' of given sizestd::vector<int>v(size);// Loops through the vector by reference// setting its elements to inputted valuesfor(auto&i:v)std::cin>>i;std::sort(v.begin(),v.end());for(autoi:v)std::cout<<i<<' ';return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Vector-Sort
You are viewing a single comment's thread. Return to all comments →
Instead of using a for loop paired with
push_back()
(which can result in allocations that could otherwise be avoided), use the explicit constructor,std::vector<T>(int)
(requiring direct initialisation via a parenthesised list) which constructs astd::vector
of a given size: