Alphabetize a roster
Before you start this master track
Typed functions and collections. Be ready to reason about references, aliasing, and iterators.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
The standard algorithm sort arranges elements in a range. begin points to the first element and end marks the position just after the last, so together they describe the whole vector. The default comparison orders these capitalized names alphabetically. front and back then reveal the first and last names.
#include <algorithm>#include <iostream>#include <string>#include <vector>int main() { std::vector<std::string> roster = {"Zed", "Ada", "Mia"}; std::sort(roster.begin(), roster.end()); std::cout << roster.front() << " / " << roster.back() << '\n';}Each example is a complete C++17 console program with its own main function and required standard-library headers. Compile and run one mission at a time.
Track the values line by line. Predict the result before running this example in your own editor.