VC++ Compiler Error Sorting Strings

I'm testing three different methods for sorting strings. Unfortunately, the MSVC compiler is having trouble compiling a direct string comparison using the overloaded relational operators < and >. I get the following compiler error:
'bool std::operator >(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'std::string'.

Should I just do a char-by-char comparison or is there an alternative?
Really need to see the relevant code!

Andy
Not sure I understand the error message, but it looks like you might be trying to compare a string with an iterator or something like that. Make sure operands are actually std::string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void isort(string* c, int l)
{
    for(int i = 0; i < l; i++)
    {
        string x = c[i];
        int j = i;

        while(j > 0 && c[j-1] > c[j])
        {
            c[j] = c[j-1];
            --j;
        }
        c[j] = x;
    }
}


It's fine with the g++ compiler BTW.
Well, this compiles OK for me with MSVC

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
#include <iostream>
#include <string>
using namespace std;

void isort(string* c, int l)
{
    for(int i = 0; i < l; i++)
    {
        string x = c[i];
        int j = i;

        while(j > 0 && c[j-1] > c[j])
        {
            c[j] = c[j-1];
            --j;
        }
        c[j] = x;
    }
}

void dump(string* c, int l)
{
    for(int i = 0; i < l; i++)
    {
        cout << c[i] << "\n";
    }
    cout << "\n";
}

int main()
{
    string test[4];
    test[0] = "one";
    test[1] = "two";
    test[2] = "three";
    test[3] = "four";

    dump(test, 4);

    isort(test, 4);

    dump(test, 4);

    return 0;
}


Though I'm not sure about the answer?

one
two
three
four

one
three
four
two


Andy
Last edited on
Maybe you forgot to include the <string> header?
I had included <string.h>. I changed it to <string> and it compiled just fine. Maybe the compiler translated <string.h> as <cstring>. Yet more craziness with the Not-so-standard libraries. As for the output, that's something else entirely.
<string.h> is the header for the standard C (not C++) string functions, like strcpy, strcat, strlen. And <cstring> the wrapper header that puts the C functions into the std namespace.
http://www.cplusplus.com/reference/cstring/

Nothing crazy about it -- it's perfectly standard!

Andy
Last edited on
Topic archived. No new replies allowed.