一个空字符串在C ++中是否包含空字符串? [英] Does an empty string contain an empty string in C++?

查看:288
本文介绍了一个空字符串在C ++中是否包含空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对我的一个问题的评论中有一个有趣的争论。我的对手声称包含是错误的。



我的推理是如果包含另一个

谁错了?



PS



我说的是 std :: string / p>

PS P.S



我不是在说子串,但即使我添加到我的问题作为一个子串,它仍然没有意义。空字符串无意义。如果允许空字符串包含在字符串中,这意味着您有无穷大的空子字符串。这是什么意思?



编辑:



我是唯一一个认为有什么问题的人函数 std :: string :: find



C ++引用清楚地说


返回值:第一个匹配的第一个字符的位置。


Ok,让我们假设有一分钟的时间,并运行这段代码:

  string empty1 =; 
string empty2 =;

int postition = empty1.find(empty2);

cout<< found \\at index<<位置<< endl;

输出为:在索引0处找到



废话部分:如何在长度为0的字符串中存在索引0?



为了能够第0个位置,字符串必须至少为1个字符。



在这种情况下,C ++提供了一个异常,这证明了我的观点:

  cout < empty2.at(empty1.find(empty2))<< endl; 

如果它真的包含一个空字符串,打印它就没有问题。

解决方案

这取决于contains的意思。





另一方面,如果你考虑到了这个问题,一个字符串作为字符集合,空字符串不能包含空字符串,因为它的元素是字符,而不是字符串。



关于集合,集合

  {2} 

是集

子集

  A = {1,2,3 } 

{2} A - all 成员的成员是数字而不是集合。



以同样的方式, {} {} ,但 {} 不是 {} 中的元素(它不能是因为它是空的)。



所以你是对的。


Just had an interesting argument in the comment to one of my questions. My opponent claims that the statement "" does not contain "" is wrong.

My reasoning is that if "" contained another "", that one would also contain "" and so on.

Who is wrong?

P.S.

I am talking about a std::string

P.S. P.S

I was not talking about substrings, but even if I add to my question " as a substring", it still makes no sense. An empty substring is nonsense. If you allow empty substrings to be contained in strings, that means you have an infinity of empty substrings. What is the point of that?

Edit:

Am I the only one that thinks there's something wrong with the function std::string::find?

C++ reference clearly says

Return Value: The position of the first character of the first match.

Ok, let's assume it makes sense for a minute and run this code:

string empty1 = "";
string empty2 = "";

int postition = empty1.find(empty2);

cout << "found \"\" at index " << position << endl;

The output is: found "" at index 0

Nonsense part: how can there be index 0 in a string of length 0? It is nonsense.

To be able to even have a 0th position, the string must be at least 1 character long.

And C++ is giving a exception in this case, which proves my point:

cout << empty2.at( empty1.find(empty2) ) << endl;

If it really contained an empty string it would had no problem printing it out.

解决方案

It depends on what you mean by "contains".

The empty string is a substring of the empty string, and so is contained in that sense.

On the other hand, if you consider a string as a collection of characters, the empty string can't contain the empty string, because its elements are characters, not strings.

Relating to sets, the set

{2}

is a subset of the set

A = {1, 2, 3}

but {2} is not a member of A - all A's members are numbers, not sets.

In the same way, {} is a subset of {}, but {} is not an element in {} (it can't be because it's empty).

So you're both right.

这篇关于一个空字符串在C ++中是否包含空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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