In: Computer Science
In C++ Lists are containers used to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. To store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous , therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. sort() function is used to sort the elements of the container by changing their positions.
nameoflist.sort() Parameters : No parameters are passed. Result : by default The elements of the container are sorted in ascending order.
example:
Input : mylist{8,5,7,4,6,1}; mylist.sort(); Output : 1,4,5,6,7,8 Input : mylist{"excellent", "nice", "cool"}; mylist.sort(); Output : cool,excellent,nice
Time Complexity : O(nlogn) : its much faster.