C ++ Что использовать для lexicographic_compare? - PullRequest
3 голосов
/ 09 января 2010

Я хочу использовать функцию lexicographic_compare в библиотеке алгоритмов на c ++.

Но я не знаю, что написать, если использовать оператор using.Например,

using std::lexicographical_compare ??

Как я могу понять это для себя в будущем?

Спасибо

Ответы [ 3 ]

3 голосов
/ 09 января 2010

Просто сделай

 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 ++ (например, Страуструп «Язык программирования C ++») от корки до корки.

1 голос
/ 09 января 2010

То, что у вас хорошо, вам также нужно будет включить заголовок алгоритма:

#include <algorithm>

Что касается того, как узнать эти вещи для себя, я настоятельно рекомендую получить копию Стандартная библиотека C ++ .

0 голосов
/ 09 сентября 2018

Пример использования, от 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 )
...