3

C++ のアルゴリズム ライブラリで関数 lexicographical_compare を使用したいと考えています。

しかし、using文までは何を書けばいいのかわかりません。例えば

using std::lexicographical_compare ??

将来、自分のためにこれをどのように理解できますか?

ありがとう

4

3 に答える 3

3

やるだけ

 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
}

将来自分自身を知るために、C++ の本 (Stroustrup の「The C++ Programming Language」など) を最初から最後まで読んでください。

于 2010-01-09T12:26:34.687 に答える
1

あなたが持っているものは問題ありません-アルゴリズムヘッダーも含める必要があります:

#include <algorithm>

これらを自分で調べる方法については、The C++ Standard Libraryのコピーを入手することを強くお勧めします。

于 2010-01-09T12:26:21.237 に答える
0

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

}

出力:

geeksforgeeks is lexicographically less than gfg

geeksforgeeks is lexicographically less  than Gfg( case-insensitive )
于 2018-09-09T11:34:14.160 に答える