strcmp与string :: compare(const string&) [英] strcmp vs. string::compare(const string &)
问题描述
对于两个stl字符串s1和s2,我得到了不同的结果
strcmp(s1.c_str(), s2.c_str())
和
s1.compare(s2)
有人可以解释这些功能的作用吗?似乎strcmp
给出了正确的权利。 (iewysiwyg)回答而compare()没有。
Hi,
For two stl strings s1 and s2, I got different results from
strcmp(s1.c_str(), s2.c_str())
and
s1.compare(s2)
can someone explain what these functions do? It seems that strcmp
gives the "right" (i.e.wysiwyg) answer while compare() does not.
推荐答案
lc **** @ yahoo.com 写道:
对于两个stl字符串s1和s2,我得到了不同的结果
strcmp (s1.c_str(),s2.c_str())
和
s1.compare(s2)
有人可以解释这些功能的作用吗?似乎strcmp
给出了正确的权利。 (iewysiwyg)回答而compare()没有。
For two stl strings s1 and s2, I got different results from
strcmp(s1.c_str(), s2.c_str())
and
s1.compare(s2)
can someone explain what these functions do? It seems that strcmp
gives the "right" (i.e.wysiwyg) answer while compare() does not.
''compare''考虑_all_内容,包括任何数字
终止的空字符。 ''c_str()''有效地截断
第一个空字符的内容。
尝试比较s1和s2的大小。
V
-
请在邮寄回复时从我的地址中删除资金
''compare'' takes into consideration _all_ contents, including any number
of the "terminating" null characters. ''c_str()'' effectively truncates
the contents at the first null character.
Try comparing the "size"s of s1 and s2.
V
--
Please remove capital As from my address when replying by mail
lc****@yahoo.com 写道:
lc****@yahoo.com wrote:
对于两个stl字符串s1和s2,我得到了不同的结果
strcmp(s1.c_str(),s2.c_str())
和
s1.compare(s2)
有人可以解释这些功能的作用吗?似乎strcmp
给出了正确的权利。 (iewysiwyg)回答而compare()没有。
Hi,
For two stl strings s1 and s2, I got different results from
strcmp(s1.c_str(), s2.c_str())
and
s1.compare(s2)
can someone explain what these functions do? It seems that strcmp
gives the "right" (i.e.wysiwyg) answer while compare() does not.
发布一个演示问题的完整程序。我将在下面显示的
不会表现出这种行为:
#include< string.h>
#include< string>
int main(void)
{
using namespace std; //快而又脏
string s1 =" foo";
string s2 =" bar";
if(!strcmp(s1.c_str(),s2.c_str()))
cout<< match \ n;
else
cout<< no match\\\
;
if(!s1.compare(s2))
cout<< match \ n;
else
cout<< no match\\\
;
string s3 =" foo";
string s4 =" foo";
if(!strcmp(s3.c_str(),s4.c_str()))
cout<< match \ n;
else
cout<< no match\\\
;
if(!s3.compare(s4))
cout<< match \ n;
else
cout<< no match\\\
;
返回0;
}
结果:
>
不匹配
不匹配
匹配
匹配
Brian
-
请为上下文引用足够的上一条消息。要从
Google执行此操作,请点击显示选项。并使用扩展的
标题中显示的回复。
Post a complete program that demonstrates the problem. The one I''ll
show below does NOT exhibit that behavior:
#include <string.h>
#include <string>
int main(void)
{
using namespace std; // quick and dirty
string s1 = "foo";
string s2 = "bar";
if (!strcmp(s1.c_str(), s2.c_str()))
cout << "match\n";
else
cout << "no match\n";
if (!s1.compare(s2))
cout << "match\n";
else
cout << "no match\n";
string s3 = "foo";
string s4 = "foo";
if (!strcmp(s3.c_str(), s4.c_str()))
cout << "match\n";
else
cout << "no match\n";
if (!s3.compare(s4))
cout << "match\n";
else
cout << "no match\n";
return 0;
}
Results:
no match
no match
match
match
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
默认用户写道:
lc **** @ yahoo.com 写道:
lc****@yahoo.com wrote:
对于两个字符串s1和s2,我得到了不同的结果
strcmp(s1.c_str(),s2.c_str())
和s1。比较(s2)
有人可以解释这些功能的作用吗?似乎strcmp
给出了正确的权利。 (iewysiwyg)回答而compare()没有。
发布一个演示问题的完整程序。我将在下面显示的那个没有表现出这种行为:
#include< string.h>
#include< string>
int main(void)
{
使用namespace std; //快又脏
字符串s1 =" foo" ;;
string s2 =" bar";
if(!strcmp(s1.c_str( ),s2.c_str()))
cout<< match \ n;
其他
cout<< no match\\\
;
if(!s1.compare(s2))
cout<< match \ n;
其他
cout<< no match\\\
;
string s3 =" foo";
string s4 =" foo";
Hi,
For two stl strings s1 and s2, I got different results from
strcmp(s1.c_str(), s2.c_str())
and
s1.compare(s2)
can someone explain what these functions do? It seems that strcmp
gives the "right" (i.e.wysiwyg) answer while compare() does not.
Post a complete program that demonstrates the problem. The one I''ll
show below does NOT exhibit that behavior:
#include <string.h>
#include <string>
int main(void)
{
using namespace std; // quick and dirty
string s1 = "foo";
string s2 = "bar";
if (!strcmp(s1.c_str(), s2.c_str()))
cout << "match\n";
else
cout << "no match\n";
if (!s1.compare(s2))
cout << "match\n";
else
cout << "no match\n";
string s3 = "foo";
string s4 = "foo";
添加
s3.append(5,0); //最后加5个零
看看会发生什么。
if(!strcmp(s3.c_str(),s4.c_str()) )
cout<< match \ n;
其他
cout<< no match\\\
;
if(!s3.compare(s4))
cout<< match \ n;
其他
cout<< no match\\\
;
返回0;
}
结果:
不匹配
没有匹配
匹配
Brian
Add
s3.append(5, 0); // add 5 zeros at the end
and see what happens.
if (!strcmp(s3.c_str(), s4.c_str()))
cout << "match\n";
else
cout << "no match\n";
if (!s3.compare(s4))
cout << "match\n";
else
cout << "no match\n";
return 0;
}
Results:
no match
no match
match
match
Brian
V
-
请在邮寄回复时从我的地址中删除资金
V
--
Please remove capital As from my address when replying by mail
这篇关于strcmp与string :: compare(const string&)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!