C++ lexicographyal_compare 有什么用?

发布于 2024-08-17 11:01:19 字数 182 浏览 6 评论 0原文

我想使用 C++ 算法库中的函数 lexicographyal_compare 。

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

using std::lexicographical_compare ??

我将来如何为自己解决这个问题?

谢谢

I wan to user the function lexicographical_compare in algorithms library in c++.

But I do not know what to write as far as the using statement. For example

using std::lexicographical_compare ??

How can I figure this out for my self in the future?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生戏 2024-08-24 11:01:19

就这样做

 using std::lexicographical_compare;

,然后(复制自 SGI STL 文档

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
}

了解自己未来,从头到尾阅读一本 C++ 书籍(例如 Stroustrup 的《The C++ 编程语言》)。

Just do

 using std::lexicographical_compare;

and then (copied from 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;
}

Alternatively

// 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
}

To know yourself in the future, read a C++ book (e.g. Stroustrup's "The C++ Programming Language") from cover to cover.

挽袖吟 2024-08-24 11:01:19

你所拥有的很好 - 你还需要包含算法标头:

#include <algorithm>

至于如何为自己找出这些东西,我强烈建议获取 C++ 标准库

What you have is fine - you will also need to include the algorithm header:

#include <algorithm>

As to how to find out these things for yourself, I strongly recommend getting a copy of The C++ Standard Library.

尾戒 2024-08-24 11:01:19

用法示例,来自 https://www.geeksforgeeks.org/lexicographyal_compare-in-cpp/< /a>

// helper function to convert all into lower case:
bool comp (char s1, char s2) {
    return tolower(s1)<tolower(s2);
}

void test_lexicographical_compare(){
    // initializing char arrays
    char one[] = "geeksforgeeks";
    char two[] = "gfg";

    // using lexicographical_compare for checking
    // is "one" is less than "two"
    if( lexicographical_compare(one, one+13, two, two+3)) {
        cout << "geeksforgeeks is lexicographically less than gfg"<<endl;

    }
    else {
        cout << "geeksforgeeks is not lexicographically less than gfg"<<endl;

    }

    // now two = "Gfg";
    strncpy(two, "Gfg", 3);
    // using lexicographical_compare for checking
    // is "one" is less than "two"
    // returns true this time as all converted into lowercase
    if( lexicographical_compare(one, one+13, two, two+3, comp)){
        cout << "geeksforgeeks is lexicographically less  ";
        cout << "than Gfg( case-insensitive )"<<endl;

    }
    else{
        cout << "geeksforgeeks is not lexicographically less ";
        cout<< "than Gfg( case-insensitive )"<<endl;
    }

}

输出:

geeksforgeeks is lexicographically less than gfg

geeksforgeeks is lexicographically less  than Gfg( case-insensitive )

Usage example, from https://www.geeksforgeeks.org/lexicographical_compare-in-cpp/

// helper function to convert all into lower case:
bool comp (char s1, char s2) {
    return tolower(s1)<tolower(s2);
}

void test_lexicographical_compare(){
    // initializing char arrays
    char one[] = "geeksforgeeks";
    char two[] = "gfg";

    // using lexicographical_compare for checking
    // is "one" is less than "two"
    if( lexicographical_compare(one, one+13, two, two+3)) {
        cout << "geeksforgeeks is lexicographically less than gfg"<<endl;

    }
    else {
        cout << "geeksforgeeks is not lexicographically less than gfg"<<endl;

    }

    // now two = "Gfg";
    strncpy(two, "Gfg", 3);
    // using lexicographical_compare for checking
    // is "one" is less than "two"
    // returns true this time as all converted into lowercase
    if( lexicographical_compare(one, one+13, two, two+3, comp)){
        cout << "geeksforgeeks is lexicographically less  ";
        cout << "than Gfg( case-insensitive )"<<endl;

    }
    else{
        cout << "geeksforgeeks is not lexicographically less ";
        cout<< "than Gfg( case-insensitive )"<<endl;
    }

}

output:

geeksforgeeks is lexicographically less than gfg

geeksforgeeks is lexicographically less  than Gfg( case-insensitive )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文