strcmp或string :: compare? [英] strcmp or string::compare?
问题描述
我想比较两个字符串。是否可以使用 strcmp
? (我试过,它似乎不工作)。 string :: compare
一个解决方案?
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?
$ c> string 至 char
?
Other than this, is there a way to compare a string
to a char
?
感谢早期的评论。我在C ++编码,是的 std :: string
像你提到的一些。
我没有发布代码,因为我想学习一般知识,它是一个很长的代码,所以这是不相干的问题。
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之间的区别,感谢你指出。现在我将尝试使用重载运算符。 string :: compare
也可以。
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 :: string
并使用 string :: compare
进行比较。
For C++, use std::string
and compare using string::compare
.
对于C,使用 strcmp
。如果您的(我的意思是您的程序)字符串(由于某些奇怪的原因)不是 nul
code> strncmp 。
For C use strcmp
. If your (i meant your programs) strings (for some weird reason) aren't nul
terminated, use strncmp
instead.
但为什么有人不是 $ c> == std :: string
?
But why would someone not use something as simple as ==
for std::string
?
这篇关于strcmp或string :: compare?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!