Compare strings

A

al

To compare two strings, not just to see if they are "equal" ("abcd"="abcd")
but tell their alphabetical order:

string a, b;
a = "abcd";
b = "bbcd";

Can all the ways below to do such comparison?

1. a > b

2. a.compare(b))

3. strcmp(a, b)

4. max(a, b)

Which one you prefer and why?

Thanks!
 
C

Cy Edmunds

al said:
To compare two strings, not just to see if they are "equal" ("abcd"="abcd")
but tell their alphabetical order:

string a, b;
a = "abcd";
b = "bbcd";

Can all the ways below to do such comparison?

1. a > b

This is good. What could be simpler?
2. a.compare(b))

Reminds me of a Fortran II IF statement, and that isn't a glowing
recommendation. :)
3. strcmp(a, b)

I don't like this one because it won't work. However if you ammend it to

strcmp(a.c_str(), b.c_str())

I still don't like it. Using char * as a string type is a leftover from C.
4. max(a, b)

This is good too, if that's what you mean. It means something different than
#1 of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top