Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Reversing: std::reverse

A high-score table starts with the best score, but std::sort puts the smallest value first. The algorithm std::reverse turns the order of a range around: the last element becomes the first, and the first one the last. Sort first, then reverse, and the largest value is at the front:

std::vector<int> scores = {640, 1250, 380, 980, 720};
std::sort(scores.begin(), scores.end());
std::reverse(scores.begin(), scores.end());

for (int i = 0; i < scores.size(); i++) {
    std::cout << i + 1 << ". " << scores[i] << std::endl;
}

This prints:

1. 1250
2. 980
3. 720
4. 640
5. 380

The order of the two lines matters. std::reverse doesn't sort anything: it only turns around the order the elements are in. On its own, it would give you 720 980 380 1250 640, the unsorted scores back to front. And if you reversed first and sorted after that, the sort would undo the reversing.

A string is a sequence of characters, so it has begin() and end() too, and the algorithms work on it as on a vector of chars:

std::string word = "stressed";
std::reverse(word.begin(), word.end());
std::cout << word << std::endl;

This prints desserts. In the Loops lesson, you printed this word backwards with a loop that counted down. std::reverse does it in one line, and it changes the string itself, so you can go on working with the reversed string. Only reverse strings without accents: a letter with an accent takes two chars, and reversing would swap them and break the letter.

In the next exercise, you will see how to sort from the largest value right away, without reversing.

Instructions

  1. 1

    The high-score table should start with the best score. Below the line that sorts scores, reverse the vector with std::reverse. The table then starts with 1. 1250 and ends with 5. 380. Keep the line with the scores as it is.

  2. 2

    The inscription on a door in the dungeon is written backwards. Before the line that prints it, reverse the string inscription with std::reverse. The program prints The door says: open sesame.

Back

2/13

  1. 1. Sorting: std::sort
  2. 2. Reversing: std::reverse
  3. 3. A Comparison Function
  4. 4. Lambdas
  5. 5. Quiz: Sort Order
  6. 6. Counting: std::count_if
  7. 7. Capturing Variables
  8. 8. Searching: std::find
  9. 9. Searching with a Lambda: std::find_if
  10. 10. Quiz: Searching
  11. 11. The Largest Element: std::max_element
  12. 12. Yes or No: std::any_of and std::all_of
  13. 13. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.