I want to sort a vector using sort algorithm in C++.
str is the name of std::vector<int> I want to sort.
What’s the difference between this:
std::sort(str.rend(),str.rbegin())
and this:
std::sort(str.begin(),str.end())
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you intend to use
std::sortto sort the string (since neitherstd::vectornorstd::stringhave asortmethod), the first statement is incorrect and leads to undefined behaviour (UB):Here,
std::sortwill attempt to dereferencestr.rend(), which is a “past the end” iterator. De-referencing such an iterator is UB.A correct use of reverse iterators would be
This would result in the string/vector being sorted in descending order.