Difference between revisions of "CPP/AlphebetizeString"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Alphabetize.cpp== <source lang="cpp"> #include <iostream> #include <vector> #include <algorithm> #include <string> void Output(std::vector<std::string> &Strings); void Alphabe…') |
(→Alphabetize.cpp) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
==Alphabetize.cpp== | ==Alphabetize.cpp== | ||
− | <source lang=" | + | <source lang="c"> |
#include <iostream> | #include <iostream> | ||
#include <vector> | #include <vector> | ||
Line 6: | Line 6: | ||
#include <string> | #include <string> | ||
+ | //Prints the vector of strings passed | ||
void Output(std::vector<std::string> &Strings); | void Output(std::vector<std::string> &Strings); | ||
+ | //sorts the vector of strings passed based on the default comparison | ||
void Alphabetize(std::vector<std::string> &Strings); | void Alphabetize(std::vector<std::string> &Strings); | ||
Line 15: | Line 17: | ||
Strings.push_back("David"); | Strings.push_back("David"); | ||
Strings.push_back("Tony"); | Strings.push_back("Tony"); | ||
− | Output(Strings); | + | |
− | + | Output(Strings); // print before being sorted | |
− | Alphabetize(Strings); | + | Alphabetize(Strings); //sort it now |
− | Output(Strings); | + | Output(Strings); //print after being sorted |
return 0; | return 0; |
Latest revision as of 23:02, 23 June 2010
Alphabetize.cpp
#include <iostream> #include <vector> #include <algorithm> #include <string> //Prints the vector of strings passed void Output(std::vector<std::string> &Strings); //sorts the vector of strings passed based on the default comparison void Alphabetize(std::vector<std::string> &Strings); int main (int argc, char *argv[]) { std::vector<std::string> Strings; Strings.push_back("Hayley"); Strings.push_back("David"); Strings.push_back("Tony"); Output(Strings); // print before being sorted Alphabetize(Strings); //sort it now Output(Strings); //print after being sorted return 0; } void Alphabetize(std::vector<std::string> &Strings) { std::sort(Strings.begin(), Strings.end()); } void Output(std::vector<std::string> &Strings) { for(unsigned int i = 0; i < Strings.size(); i++) { std::cout << Strings[i] << std::endl; } }