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
#include
#include
void print(const std::string& item)
{
std::cout << item << std::endl;
}
int main()
{
std::set
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
You can use the sort function:
vector
sort(s.begin(),s.end());