← Quiz: STL Algorithms: all 10 questions
Quiz: STL Algorithms · Question 7 of 10
What does this print? (code)
What does this print?
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> names = {"Ravi", "Asha", "Om", "Meena"};
sort(names.begin(), names.end(), [](const string &a, const string &b) {
return a.size() < b.size();
});
cout << names[0] << " " << names[3] << "\n";
return 0;
}
- A.Asha Ravi
- B.Om Meena
- C.Meena Om
- D.Asha Meena
Answer & explanation
Correct answer: Om Meena
The lambda compares lengths, so the shortest name (Om, 2 letters) comes first and the longest (Meena, 5) last.
Can you get the rest right without peeking?
Take the timed Quiz: STL Algorithms quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →