Embercode
✕
Character Arrays and StringsQuick quiz

Ordering

6 questions. Covers Lexicographic comparison.

Questions 1 to 5 of 6

  1. 1. What does this program print? Words are separated by single spaces. ``cpp #include <algorithm> #include <iostream> #include <ranges> #include <string> #include <vector> int main() { std::vector<std::string> v = {"pear", "Peach", "apple", "Apple", "10", "9"}; std::ranges::sort(v); for (const auto& s : v) std::cout << s << " "; std::cout << "\n"; } ``
  2. 2. What does this program print? Words are separated by single spaces. ``cpp #include <algorithm> #include <cctype> #include <iostream> #include <ranges> #include <string> #include <vector> bool ci_less(const std::string& a, const std::string& b) { return std::ranges::lexicographical_compare(a, b, {}, [](char c) { return std::tolower(static_cast<unsigned char>(c)); }, [](char c) { return std::tolower(static_cast<unsigned char>(c)); }); } int main() { std::vector<std::string> v = {"Cat", "dog", "cat", "CAT", "Bird"}; std::ranges::stable_sort(v, ci_less); for (const auto& s : v) std::cout << s << " "; std::cout << "\n"; } ``
  3. 3. True or false: std::ranges::sort guarantees to keep equal items in their input order, so Apple stays before apple after a case-insensitive sort.
  4. 4. True or false: std::string("apple") < std::string("apples") is true.
  5. 5. Why does a plain std::ranges::sort put Zebra before apple?

5 question(s) left on this page.