compre two strings..

N

Nawaz

how to compare two string written below.

string str1;

string str2;

thanks for answer;
 
K

Kai-Uwe Bux

Nawaz said:
how to compare two string written below.

string str1;

string str2;

For equality: str1 == str2
or: str1 != str2

For lexicographic comparison: str1 < str2
or: str1 <= str2
or: str1 > str2
or: str1 >= str2

Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Geoff said:
Compare two strings or compare two pointers to strings?

In the OP, there is not the slightest hint at pointers to strings. So what
brings up that question? (Maybe your crystal ball is working, mine is in
repair:)


Best

Kai-Uwe Bux
 
N

Nawaz

Nawaz ha scritto:





str1 == str2

--
Christian Hackl
(e-mail address removed)

Milano 2008/2009 -- L'Italia chiamò, sì!


no sir in real I want a answer of a question that the char pointer
can compare by strcmp like this :


char * str1;
char * str2;
strcmp(str1,str2);

but why we can not compare two string of string datatype in this way;

string str1;
string str2;
?

thanks for reply;
 
K

Kai-Uwe Bux

Nawaz said:
no sir in real I want a answer of a question that the char pointer
can compare by strcmp like this :


char * str1;
char * str2;
strcmp(str1,str2);

but why we can not compare two string of string datatype in this way;

string str1;
string str2;

a) you can: strcmp( str1.c_str(), str2.c_str() );

b) why would you want to that? After all:

str1 < str2

is equivalent to

strcmp( str1.c_str(), str2.c_str() ) < 0


Best

Kai-Uwe Bux
 
N

Nawaz

a) you can: strcmp( str1.c_str(), str2.c_str() );

b) why would you want to that? After all:

  str1 < str2

is equivalent to

  strcmp( str1.c_str(), str2.c_str() ) < 0

Best

Kai-Uwe Bux

because I want a valid logical answer that the char pointer is a
string

char * str //string

and

string str //string

so what is the defference between them?
 
P

Paul N

a) you can: strcmp( str1.c_str(), str2.c_str() );

b) why would you want to that? After all:

  str1 < str2

is equivalent to

  strcmp( str1.c_str(), str2.c_str() ) < 0

In other words, C++ has some neat features, including "operator
overloading", which allow strings to be handled in a fairly nice way.
Unlike C, which is fairly simple and so needs lots of messy stuff like
strcmp to do things which seem straight-forward.

Mind you, I've never used C++ strings. Maybe one day...

Paul.
 
K

Kai-Uwe Bux

Nawaz said:
because I want a valid logical answer that the char pointer is a
string

Huh? I don't understand that sentence.
char * str //string

and

string str //string

so what is the defference between them?

Differences include:

a) std::string stores the length whereas char* has to rely on a sentinel-
value (0) to mark the end of the string. This is a typical speed-memory
trade-off. Consequently, some operations are faster for std::string. On the
other hand char* is more light-weight and can perform better in some
circumstances.

b) char* requires manual memory management whereas std::string does that for
you behind the scenes. Manual memory management is difficult. In particular,
it is hard to write classes with several char* members in an exception-safe
way. There are no such problems with std::string.

c) Operations on strings, such as appending words, are syntactically nicer
with std::string. The corresponding functions for char* are considered
unsafe by some because they require that you pay close attention to the size
of the buffers you provide.


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Geoff said:
Yes, as the thread has shown, my crystal ball was seeing something
along the C vs C++ string management.

True; and very, very impressive.


Best

Kai-Uwe Bux
 
F

Fred Zwarts

Nawaz said:
because I want a valid logical answer that the char pointer is a
string

char * str //string

and

string str //string

so what is the defference between them?

The difference is that "char * str" does not define a string, but a pointer to a char.
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top