strcmp or string::compare?(strcmp 或 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?
除此之外,有没有办法将 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
终止,请使用 strncmp
代替.
For C use strcmp
. If your (i meant your programs) strings (for some weird reason) aren't nul
terminated, use strncmp
instead.
但是为什么有人不会为 std::string
使用像 ==
这样简单的东西?
But why would someone not use something as simple as ==
for std::string
?
这篇关于strcmp 或 string::compare?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:strcmp 或 string::compare?


- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01