lexicographical_compare

Hello there. I was doing some coding today, and i needed a function to see if 2 words compare lexicographically so after 5 hours of surfin internet i found one function, lexicographical_compare which appears to be perfect for my program. So, its my first time using this function and it seems like im doing it wrong. Hers my program:(part you need is at the bottom)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    int n, i, j=0, a[50];
    string dete[20];
    cout << "Unesite broj dece: ";
    cin >> n;
    for(i=0;i<n;i++){ // for za unos imena
        cout << "Unesite ime "<< (i+1) << ". deteta: ";
        cin >> dete[i];
    }
    for(i=0;i<n;i++){ // for za veliko u malo
        while (dete[i][j] != '\n') {
            dete[i][j]=tolower(dete[i][j]);
            j++;
        }
        j=0;
    }
    for(i=0;i<n;i++){ // for za palindrom
        if (dete[i] == string(dete[i].rbegin(), dete[i].rend())) {
            a[i]=1;
        }
        else a[i]=0;
    }
    j=0;
    for(i=0;i<n;i++){
        while(j<n){
            if(i==j) continue;
            else if(dete[i]==dete[j]) a[i]++;
        }
        j=0;
    }
    j=0;
    for(i=0;i<n;i++){ // for za leksikografiju
        while(j<n){
            if(i==j) continue;
            if((lexicographical_compare(dete[i],dete[i].length()-1, dete[j],dete[j].length()-1) && (dete[i].length() > dete[j].length()))) a[i]++;
        }
    }
    cout << dete[0] << " BROJ: " << a[0];
    return 1;
}


Errors i get are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\Users\Plavsa\Desktop\Projekti\turisti\main.cpp||In function 'int main()':|

C:\Users\Plavsa\Desktop\Projekti\turisti\main.cpp|44|error: no matching function for call to 'lexicographical_compare(std::string&, std::basic_string<char>::size_type, std::string&, std::basic_string<char>::size_type)'|

C:\Users\Plavsa\Desktop\Projekti\turisti\main.cpp|44|note: candidates are:|

d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algobase.h|1086|note: template<class _II1, class _II2> bool std::lexicographical_compare(_II1, _II1, _II2, _II2)|

d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algobase.h|1086|note:   template argument deduction/substitution failed:|

C:\Users\Plavsa\Desktop\Projekti\turisti\main.cpp|44|note:   deduced conflicting types for parameter '_II1' ('std::basic_string<char>' and 'unsigned int')|

d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algobase.h|1120|note: template<class _II1, class _II2, class _Compare> bool std::lexicographical_compare(_II1, _II1, _II2, _II2, _Compare)|

d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algobase.h|1120|note:   template argument deduction/substitution failed:|

|44|note:   deduced conflicting types for parameter '_II1' ('std::basic_string<char>' and 'unsigned int')|
||=== Build finished: 1 errors, 2 warnings (0 minutes, 0 seconds) ===|


I cant see where it goes wrong, tried with char-type string but it didnt help.

Regards,
Plavsa
lexographical_compare takes iterators, not strings.
Could you explain that please?
Last edited on
std::lexicographical_compare is for when you have a iterator range and you want to compare it the same way string < string or vector < vector does (or when you have a custom comparison function)

Since you're comparing whole strings, just use operator< (or operator>, as needed): if (dete[i] < dete[j])
Topic archived. No new replies allowed.