Biorąc pod uwagę dwa ciągi, jak sprawdzić, czy dwa ciągi są równe, czy nie.
Przykłady:
Input : ABCD, XYZ Output : ABCD is not equal to XYZ XYZ is greater than ABCD Input : Geeks, forGeeks Output : Geeks is not equal to forGeeks forGeeks is greater than Geeks>
Problem ten można rozwiązać za pomocą dowolnej z dwóch poniższych metod
- Operatory relacyjne w C++
CPP
sql wybierz jako
// CPP code to implement relational> // operators on string objects> #include> using> namespace> std;> void> relationalOperation(string s1, string s2)> {> >if> (s1 != s2)> >{> >cout << s1 <<>' is not equal to '> << s2 << endl;> >if> (s1>s2)> >cout << s1 <<>' is greater than '> << s2 << endl;> >else> >cout << s2 <<>' is greater than '> << s1 << endl;> >}> >else> >cout << s1 <<>' is equal to '> << s2 << endl;> }> // Driver code> int> main()> {> >string s1(>'Geeks'>);> >string s2(>'forGeeks'>);> >relationalOperation(s1, s2);> >string s3(>'Geeks'>);> >string s4(>'Geeks'>);> >relationalOperation(s3, s4);> >return> 0;> }> |
>
>Wyjście
Geeks is not equal to forGeeks forGeeks is greater than Geeks Geeks is equal to Geeks>
Złożoność czasowa: O(min(n,m)) gdzie n i m to długość strun.
Przestrzeń pomocnicza: O(max(n,m)) gdzie n i m to długość strun.
sortowanie tablicowe Java
Dzieje się tak, ponieważ przekazanie ciągu znaków do funkcji powoduje utworzenie własnej kopii na stosie.
- std:: Porównaj()
CPP
// CPP code perform relational> // operation using compare function> #include> using> namespace> std;> void> compareFunction(string s1, string s2)> {> >// comparing both using inbuilt function> >int> x = s1.compare(s2);> >if> (x != 0) {> >cout << s1> ><<>' is not equal to '> ><< s2 << endl;> >if> (x>0)> >cout << s1> ><<>' is greater than '> ><< s2 << endl;> >else> >cout << s2> ><<>' is greater than '> ><< s1 << endl;> >}> >else> >cout << s1 <<>' is equal to '> << s2 << endl;> }> // Driver Code> int> main()> {> >string s1(>'Geeks'>);> >string s2(>'forGeeks'>);> >compareFunction(s1, s2);> >string s3(>'Geeks'>);> >string s4(>'Geeks'>);> >compareFunction(s3, s4);> >return> 0;> }> |
>
>Wyjście
Dekodowanie base64 w js
Geeks is not equal to forGeeks forGeeks is greater than Geeks Geeks is equal to Geeks>
Złożoność czasowa: O(min(n,m)) gdzie n i m to długość strun.
Przestrzeń pomocnicza: O(max(n,m)) gdzie n i m to długość strun.
Dzieje się tak, ponieważ przekazanie ciągu znaków do funkcji powoduje utworzenie własnej kopii na stosie.
Różnice między operatorami relacyjnymi C++ a funkcją porównania () :-
- Compare() zwraca liczbę typu int, podczas gdy operatory relacji zwracają wartość logiczną, tj. prawdę lub fałsz.
- Pojedynczy operator relacyjny jest unikalny dla określonej operacji, podczas gdy funkcja Compare() może samodzielnie wykonywać wiele różnych operacji, w zależności od typu przekazanych argumentów.
- Możemy porównać dowolny podciąg w dowolnej pozycji w danym ciągu za pomocą funkcji Compare(), co w przeciwnym razie wymaga długiej procedury wyodrębniania ciągu słowo po słowie do porównania przy użyciu operatorów relacyjnych.
Przykład:-
- Korzystanie z porównania()
// Compare 3 characters from 3rd position // (or index 2) of str1 with 3 characters // from 4th position of str2. if (str1.compare(2, 3, str2, 3, 3) == 0) cout<<'Equal'; else cout<<'Not equal';>
- Korzystanie z operatora relacyjnego
for (i = 2, j = 3; i <= 5 && j <= 6; i++, j++) { if (s1[i] != s2[j]) break; } if (i == 6 && j == 7) cout << 'Equal'; else cout << 'Not equal';> Powyższy przykład wyraźnie pokazuje, jak to zrobić porównywać() zmniejsza dużo dodatkowego przetwarzania, dlatego wskazane jest użycie go podczas porównywania podciągów w jakiejś pozycji, w przeciwnym razie oba działają prawie w ten sam sposób.