C++ strcmp 或字符串::比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5492485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:16:22  来源:igfitidea点击:

strcmp or string::compare?

c++cstringcharstring-comparison

提问by Perex19

I want to compare two strings. Is it possible with strcmp? (I tried and it does not seem to work). Is string::comparea solution?

我想比较两个字符串。有可能strcmp吗?(我试过了,它似乎不起作用)。是string::compare解决方案吗?

Other than this, is there a way to compare a stringto a char?

除此之外,有没有办法将 astring与 a进行比较char



Thanks for the early comments. I was coding in C++ and yes it was std::stringlike 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++ 编码,是的,std::string就像你们中的一些人提到的那样。我没有发布代码,因为我想学习一般知识,它是一段很长的代码,因此与问题无关。

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::compareworked too.

我想我了解了 C++ 和 C 之间的区别,感谢您指出这一点。我现在将尝试使用重载运算符。顺便说一句也string::compare奏效了。

回答by Sadique

For C++, use std::stringand compare using string::compare.

对于 C++,使用std::string和比较使用string::compare.

For C use strcmp. If your (i meant your programs) strings (for some weird reason) aren't nulterminated, use strncmpinstead.

对于 C 使用strcmp. 如果您的(我的意思是您的程序)字符串(出于某种奇怪的原因)没有nul终止,请strncmp改用。

But why would someone notuse something as simple as ==for std::string?

但是为什么有人使用像==for这样简单的东西std::string呢?

回答by Mark B

Assuming you mean std::string, why not use the overloaded operators: str1 == str2, str1 < str2?

假设您的意思是std::string,为什么不使用重载运算符:str1 == str2, str1 < str2

回答by ildjarn

See std::basic_string::compareand std::basic_stringoperators reference(in particular, there exists operator==, operator!=, operator<, etc.). What else do you need?

std::basic_string::comparestd::basic_string运营商参考(特别是,存在operator==operator!=operator<等等)。你还需要什么?

回答by doron

When using C++ use C++ functions viz. string::compare. When using C and you are forced to use char*for string, use strcmp

使用 C++ 时,请使用 C++ 函数即。string::compare. 当使用 C 并且您被迫使用char*字符串时,请使用strcmp

回答by Pete Wilson

By your question, "is there a way to compare a string to a char?" do you mean "How do I find out if a particular char is contained in a string?" If so, the the C-library function:

通过您的问题,“有没有办法将字符串与字符进行比较?” 你的意思是“我如何找出一个特定的字符是否包含在一个字符串中?” 如果是这样,则 C 库函数:

  char *strchr(const char *s, int c);

will do it for you.

会为你做的。

-- pete

——皮特

回答by sehe

std::string can contain (and compare!) embedded null characters.

std::string 可以包含(和比较!)嵌入的空字符。

are*comp(...) will compare c-style strings, comparing up to the first null character (or the specified max nr of bytes/characters)

are*comp(...) 将比较 c 样式的字符串,最多比较第一个空字符(或指定的最大字节数/字符数)

string::compare is actually implemented as a template basic_string so you can expect it to work for other types such as wstring

string::compare 实际上是作为模板 basic_string 实现的,因此您可以期望它适用于其他类型,例如 wstring

On the unclear phrase to "compare a string to a char" you can compare the char to *string.begin() or lookup the first occurrence (string::find_first_of and string::find_first_not_of)

在不清楚的短语“将字符串与字符进行比较”时,您可以将字符与 *string.begin() 进行比较或查找第一次出现(string::find_first_of 和 string::find_first_not_of)

Disclaimer: typed on my HTC, typos reserved :)

免责声明:在我的 HTC 上输入,保留错别字:)