home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / Comparison.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.8 KB  |  53 lines

  1. //: C21:Comparison.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // The STL range comparison algorithms
  7. #include "PrintSequence.h"
  8. #include <vector>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <string>
  12. using namespace std;
  13.  
  14. int main() {
  15.   // strings provide a convenient way to create
  16.   // ranges of characters, but you should 
  17.   // normally look for native string operations:
  18.   string s1("This is a test");
  19.   string s2("This is a Test");
  20.   cout << "s1: " << s1 << endl 
  21.     << "s2: " << s2 << endl;
  22.   cout << "compare s1 & s1: " 
  23.     << equal(s1.begin(), s1.end(), s1.begin())
  24.     << endl;
  25.   cout << "compare s1 & s2: " 
  26.     << equal(s1.begin(), s1.end(), s2.begin())
  27.     << endl;
  28.   cout << "lexicographical_compare s1 & s1: " <<
  29.     lexicographical_compare(s1.begin(), s1.end(),
  30.       s1.begin(), s1.end()) <<  endl;
  31.   cout << "lexicographical_compare s1 & s2: " <<
  32.     lexicographical_compare(s1.begin(), s1.end(),
  33.       s2.begin(), s2.end()) << endl;
  34.   cout << "lexicographical_compare s2 & s1: " <<
  35.     lexicographical_compare(s2.begin(), s2.end(),
  36.       s1.begin(), s1.end()) << endl;
  37.   cout << "lexicographical_compare shortened " 
  38.     "s1 & full-length s2: " << endl;
  39.   string s3(s1);
  40.   while(s3.length() != 0) {
  41.     bool result = lexicographical_compare(
  42.       s3.begin(), s3.end(), s2.begin(),s2.end());
  43.     cout << s3 << endl << s2 << ", result = " 
  44.       << result << endl;
  45.     if(result == true) break;
  46.     s3 = s3.substr(0, s3.length() - 1);
  47.   }
  48.   pair<string::iterator, string::iterator> p =
  49.     mismatch(s1.begin(), s1.end(), s2.begin());
  50.   print(p.first, s1.end(), "p.first", "");
  51.   print(p.second, s2.end(), "p.second","");
  52. } ///:~
  53.