comparison.cpp

来自「Think in C++ 第二版源码」· C++ 代码 · 共 53 行

CPP
53
字号
//: C21:Comparison.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// The STL range comparison algorithms

#include "PrintSequence.h"

#include <vector>

#include <algorithm>

#include <functional>

#include <string>

using namespace std;



int main() {

  // strings provide a convenient way to create

  // ranges of characters, but you should 

  // normally look for native string operations:

  string s1("This is a test");

  string s2("This is a Test");

  cout << "s1: " << s1 << endl 

    << "s2: " << s2 << endl;

  cout << "compare s1 & s1: " 

    << equal(s1.begin(), s1.end(), s1.begin())

    << endl;

  cout << "compare s1 & s2: " 

    << equal(s1.begin(), s1.end(), s2.begin())

    << endl;

  cout << "lexicographical_compare s1 & s1: " <<

    lexicographical_compare(s1.begin(), s1.end(),

      s1.begin(), s1.end()) <<  endl;

  cout << "lexicographical_compare s1 & s2: " <<

    lexicographical_compare(s1.begin(), s1.end(),

      s2.begin(), s2.end()) << endl;

  cout << "lexicographical_compare s2 & s1: " <<

    lexicographical_compare(s2.begin(), s2.end(),

      s1.begin(), s1.end()) << endl;

  cout << "lexicographical_compare shortened " 

    "s1 & full-length s2: " << endl;

  string s3(s1);

  while(s3.length() != 0) {

    bool result = lexicographical_compare(

      s3.begin(), s3.end(), s2.begin(),s2.end());

    cout << s3 << endl << s2 << ", result = " 

      << result << endl;

    if(result == true) break;

    s3 = s3.substr(0, s3.length() - 1);

  }

  pair<string::iterator, string::iterator> p =

    mismatch(s1.begin(), s1.end(), s2.begin());

  print(p.first, s1.end(), "p.first", "");

  print(p.second, s2.end(), "p.second","");

} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?