⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exe23.cpp

📁 practice c++, it is from the book http://www.amazon.com/Schaums-Outline-Programming-John-Hubbard
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Example E.23 on page 378
//  Testing the lexicographical_compare() algorithm

#include <algorithm>
#include <iostream>
using namespace std;
void test(char*,int,char*,int);

int main()
{ char* s1="COMPUTER";
  char* s2="COMPUTABLE";
  char* s3="COMPUTE";
  test(s1,3,s2,3); 
  test(s1,8,s2,10); 
  test(s1,8,s3,7); 
  test(s2,10,s3,7); 
  test(s1,7,s3,7); 
}

char* sub(char*,int);

void test(char* s1, int n1, char* s2, int n2)
{ bool lt=lexicographical_compare(s1,s1+n1,s2,s2+n2);
  bool gt=lexicographical_compare(s2,s2+n2,s1,s1+n1);
  if (lt) cout << sub(s1,n1) << " < " << sub(s2,n2) << "\n";
  else if (gt) cout << sub(s1,n1) << " > " << sub(s2,n2) << "\n";
  else cout << sub(s1,n1) << " == " << sub(s2,n2) << "\n";  
}

char* sub(char* s, int n)
{ char* buffer = new char(n+1);
  strncpy(buffer,s,n);
  buffer[n] = 0;
  return buffer;
}

⌨️ 快捷键说明

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