strcmp 或 string::compare? [英] strcmp or string::compare?

查看:25
本文介绍了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?

除此之外,有没有办法将 stringchar 进行比较?

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆