问题描述
我要比较两个字符串。是否有可能与 STRCMP
? (我想,它似乎不工作)。为字符串::比较
的解决方案?
I want to compare two strings. Is it possible with strcmp
? (I tried and it does not seem to work). Is string::compare
a solution?
除此之外,有没有办法来比较字符串
到字符
?
Other than this, is there a way to compare a string
to a char
?
感谢争取早日意见。我是用C ++编码和是它为std :: string像有些人提及。
我没有贴code,因为我想学的一般知识,这是一个pretty长code,所以这是无关的问题。
Thanks for the early comments. I was coding in C++ and yes it was std::string like some of you mentioned.I didn't post the code because I wanted to learn the general knowledge and it is a pretty long code, so it was irrelevant for the question.
我觉得我学会了C ++和C中的差异,感谢指出了这一点。我会尝试,现在使用重载操作符。顺便说一句字符串::比较工作太。
I think I learned the difference between C++ and C, thanks for pointing that out. And I will try to use overloaded operators now. And by the way string::compare worked too.
推荐答案
有关C ++中,使用的std ::字符串
,并使用字符串比较::比较
。
For C++, use std::string
and compare using string::compare
.
对于C使用 STRCMP
。如果你(的我的意思你的程序的)字符串(由于某种奇怪的原因的)不是 NUL
终止,使用 STRNCMP
代替。
For C use strcmp
. If your (i meant your programs) strings (for some weird reason) aren't nul
terminated, use strncmp
instead.
但是,为什么会有人的不的使用简单的东西如 ==
为的std ::字符串
?
But why would someone not use something as simple as ==
for std::string
?
这篇关于STRCMP或字符串::比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!