C++ lexicographical_compare 有什么用?

标签 c++ function lexicographic

我想使用 c++ 算法库中的函数 lexicographical_compare。

但就using语句而言,我不知道该写什么。例如

using std::lexicographical_compare ??

我以后该如何解决这个问题?

谢谢

最佳答案

就这样

 using std::lexicographical_compare;

然后(从 SGI STL Doc 复制)

int main()
{
  int A1[] = {3, 1, 4, 1, 5, 9, 3};
  int A2[] = {3, 1, 4, 2, 8, 5, 7};
  int A3[] = {1, 2, 3, 4};
  int A4[] = {1, 2, 3, 4, 5};

  const int N1 = sizeof(A1) / sizeof(int);
  const int N2 = sizeof(A2) / sizeof(int);
  const int N3 = sizeof(A3) / sizeof(int);
  const int N4 = sizeof(A4) / sizeof(int);

  bool C12 = lexicographical_compare(A1, A1 + N1, A2, A2 + N2);
  bool C34 = lexicographical_compare(A3, A3 + N3, A4, A4 + N4);

  cout << "A1[] < A2[]: " << (C12 ? "true" : "false") << endl;
  cout << "A3[] < A4[]: " << (C34 ? "true" : "false") << endl;
}

或者

// no using statement

int main()
{
   //... same as above
   bool C12 = std::lexicographical_compare(A1, A1 + N1, A2, A2 + N2);
   bool C34 = std::lexicographical_compare(A3, A3 + N3, A4, A4 + N4);
   //... same as above
}

要了解 future 的自己,请从头到尾阅读一本 C++ 书籍(例如 Stroustrup 的“The C++ Programming Language”)。

关于C++ lexicographical_compare 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2033311/

相关文章:

c++ - 使用 UTF8

c++ - 如何将 .hex 文件反编译为 Arduino 的 C++?

c - 如何解决 "control reaches end of non-void function"警告?

c++ - 可选引用成员——这可能吗?

c++ - 避免双重包含 : Preprocessor directive vs. makefile

javascript - Canvas 随重力交替形状

python 函数 for 字典中的循环

java - 从字符串 s 中打印给定大小 k 的字典顺序最小和最大子字符串

python - 忽略大小写的字符串排序列表

string - 如何通过反转子字符串找到字典序最小的字符串?