You could use std::set or std::multiset (if you will allow repeated items) of strings, and it will keep the items sorted automatically (you could even change the sorting criteria if you want).
#include <iostream> #include <set> #include <algorithm> void print(const std::string& item) { std::cout << item << std::endl; } int main() { std::set<std::string> sortedItems; for(int i = 1; i <= 5; ++i) { std::string name; std::cout << i << ". "; std::cin >> name; sortedItems.insert(name); } std::for_each(sortedItems.begin(), sortedItems.end(), &print); return 0; }
input:
- Gerardo
- Carlos
- Kamilo
- Angel
- Bosco
output:
Angel Bosco Carlos Gerardo Kamilo