struct opCmp confustion

Charles Hixson charleshixsn at earthlink.net
Sun Aug 26 12:15:09 PDT 2012


On 08/26/2012 10:14 AM, Era Scarecrow wrote:
> On Sunday, 26 August 2012 at 16:27:32 UTC, Charles Hixson wrote:
>> Currently the code is:
>>
>> struct Triplet
>> { string wrd1;
>> string wrd2;
>> string wrd3;
>> int val = 1;
>>
>>...
>>
>> What was I doing wrong?
>
> I would think it's you attempting to compare a string. Try using cmp?.
>
> untested:
>
> import std.algorithm : cmp;
>
> struct Triplet {
> string wrd1, wrd2, wrd3;
>
> int opCmp(ref const Triplet t) const {
> int result1 = cmp(wrd1, t.wrd1);
> int result2 = cmp(wrd2, t.wrd2);
> int result3 = cmp(wrd3, t.wrd3);
>
> if (result1) return result1;
> if (result2) return result2;
> if (result3) return result3;
>
> return 0;
> }
> }

Thank you.

So string doesn't respond to opCmp.  Pity.


More information about the Digitalmars-d-learn mailing list