CPP/AlphebetizeString

From ProgrammingExamples
< CPP
Revision as of 23:02, 23 June 2010 by 99.75.106.101 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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;
  }
}