View Single Post
Old Apr 3rd, 2008, 3:14 AM   #6
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 2 mbd is on a distinguished road
Re: C++ file sorting

here is a start. it copies standard input into a vector, sorts it, then copies the vector to standard output. this just uses a lexicographical string comparison.
c++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <vector>
  3. #include <iterator>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. int main()
  8. {
  9. std::vector< std::string > v;
  10. std::copy(std::istream_iterator< std::string >(std::cin), std::istream_iterator< std::string >(), std::back_insert_iterator< std::vector <std::string > >(v));
  11. std::sort(v.begin(), v.end());
  12. std::copy(v.begin(), v.end(), std::ostream_iterator< std::string >(std::cout, "\n"));
  13. return 0;
  14. }
a good place for you to begin would to be overriding the < operator for std::string.
mbd is offline   Reply With Quote