Compare two strings or string objects for equality in c\c++

Source: Internet
Author: User
Tags strcmp
compare two strings or string objects for equality in c\c++In the process of writing a program, you often encounter situations where you want to compare two strings for equality. If the object to be compared is a char* string, the strcmp is returned as a negative number when the char* is used with the Int (const char* s1,const s2 S1<S2);
When S1==s2, the return value = 0;
Returns a positive number when s1>s2.
That is: two strings are compared from left to right by character (compared to the size of the ASCII value) until there are different characters or "". Such as:
"A" < "B" "a" > "a" "Computer" > "Compare"
Special NOTE: strcmp (const char *s1,const char * s2) which can only compare strings, can be used to compare two string constants, or compare array and string constants, not other forms of parameters such as numbers.
The ANSI standard stipulates that the return value is positive, negative, 0. The exact value is dependent on a different C implementation.
If the object to be compared is two strings, the function compare () is used.         To compare string S1 and S2, write as: S1.compare (S2), which is equal if the return value is 0. Note: When comparing two strings, you cannot take advantage of the symbol "= =", the "= =" Symbol compares two string addresses are equal, before making a mistake on this issue, I remember. I remember.

If you compare two characters, you can use "= =" Direct comparison ~

However, when comparing two string objects with "= =", the return value of the expression is 1, equal to 0. For a simple example:

#include <iostream>
#include <string>

using namespace std;

int main () {
	char a[] = "AAA", b[]= "AAA";
	String A = "AAA", B = "AAA";
	The values of cout << "*a and *b respectively are:" <<*a << "," << *b << Endl;
	cout << "*" AAA "value is:" << * "AAA" << Endl;
	cout << "Use = = Compare a,b two strings, the result is (equal to 1, unequal to 0):" << (a==b) << Endl;
	cout << "uses strcmp () to compare a,b two strings, resulting in (equal to 0, not varying from 0):" <<strcmp (a,b) << Endl;
	cout << "Use = = Compare A,b Two string, the result is (equal to 1, unequal to 0):" << (a==b) << Endl;
	cout << "uses compare () to compare a,b two strings, resulting in (equal to 0, not varying 0):" << a.compare (B) << Endl;
	int x = ten;
	void* p = &x;
	cout << sizeof (void*) << "Address" << p << Endl;
	return 0;
}

The output results are:


As you can see from the program, the reason for comparing strings with "= =" is that the string itself is an address, for example:

Char a[]= "AAA";

It's easy to understand that a is a pointer to a string, but you should also know that the "AAA" string that contains quotes is also an address that points to the first element of the string, so the result of * "AAA" is a.

For string objects, there are not so many considerations ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.