c++ - 字符串是否到处都包含空子字符串?

标签 c++ string

这个问题来自于 this answer 上的讨论。 .

简而言之:答案 (0x499602D2) 的作者声称(我现在知道是正确的)如果不跳过空格,但下一个字符是空格,则除字符外的所有提取都将失败。

我对此提出质疑,因为我认为提取 string 应该不会失败,因为流包含一个由开头的空白字符分隔的空字符串。

这发展成为一般性讨论是否在字符串中的任何位置存在空字符串,例如在字符串 "ab"ab 之间(我说是,0x499602D2 说不是)。 0x499602D2 建议我把它放在一个问题中,所以我在这里。

我从该线程(包括聊天部分)复制我的立场的主要论点:

Let's first look at the constant for an empty string. In C and C++, the content is delimited by quotes at the beginning and end. So what does the empty string look like? You know it: "". You see, after the initial quote (delimiter) directly follows the final quote (delimiter). The empty string is in between the two quotes, which follow directly on each other, because the empty string has no characters. Also look at the C representation. That is the sequence of characters, followed by the delimiter '\0'. So what is the representation of the empty string? Well, the characters of the empty string followed by the delimiter. Which means, the first character is the delimiter (that is, exactly as in the stream case). Now consider the concatenation of strings, where e.g. the first string is "a", the second string is empty, and the third string is "b". So what is the concatenation? Well, "ab". So clearly there's an empty string between the a and the b in "ab" (we explicitly put it there!). And of course that is true also before the a and after the b. That is, there's an empty string (or two, or a million) between any two characters of a string.

An empty string has no characters, and between consecutive characters, there are no characters. Therefore between two characters there's an empty string. Also see the other arguments I've given before. In addition, consider regular expressions which match the empty string: They also match everywhere. For example, /ab*c/ matches "ac" because b* matches the empty string between a and c

There's an empty string (i.e, no characters) before the delimiter (space), just as in the C representation of the empty string, there are no characters before the \0 delimiter. Also note that readline also works the same with the \n delimiter: If the \n follows immediately, it doesn't fail but gives an empty string.

我感觉无法确定0x499602D2在讨论中的主要论点,所以我不去尝试,以免在选择时无意中产生不公平。您应该能够在评论中看到它们(也可能在聊天室中——我不知道是否每个人都可以访问)。 @0x499602D2:如果你愿意,你也可以自己在这一段之后添加你的主要论点。

与此相关的实际问题是:如果分隔符前没有字符(如字符串的 operator>> 那样),设计良好的字符串提取函数是否应该失败,或者成功并返回一个空字符串(如 readline 那样)?

最佳答案

定理

字符串s中任意位置有一个空字符串ε

证明

1.如果 |s| = 0(s 的长度为零),然后 s = ε,并且声明成立。

2.如果|s| > 0,则 s 有两个边缘位置:一个在其第一个符号之前,另一个在最后一个符号之后。由于ε是连接操作的单位元素,即εs = = s,声明对开始和结束位置都成立。

3.如果|s| > 1,那么 s 可以写成两个非空字符串的串联:s = pq,其中 |p| > 0 和 |q| > 0. 利用ε的恒等元属性,pεq = (pε) q = pq = s,这意味着声明适用于 s 中的位置,将其分为 >pq。这个划分的位置可以是 s 的任何内部位置,所以声明也适用于每个内部位置。

推论

恒等元属性意味着 ε = εε = εεε = etc 替换ε后重复上述证明em> 与ε^n,其中n 为正整数,我们发现在任何字符串的任何位置都有无限多个空字符串。

注意事项

此处“位置”一词的意思是“插入符号位置”(text insertion cursor 位置)。插入符号可以放在第一个符号之前(索引:0)、连续符号之间和最后一个符号之后(索引:|s|)。插入符位置的数量是 |s| + 1.

以上证明表明,符号之间的这些“零宽度间隙”可以想象为填充了任意数量的字符串。 (这就像空集是每个集合的子集一样奇怪,包括它自己。)

关于c++ - 字符串是否到处都包含空子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647980/

相关文章:

c++ - 如何避免与 `asio::ip::tcp::iostream` 的数据竞争?

java - 获取字符串 "600sp"整数部分的最佳方法?

c++ - 将非 null 终止的 vector<char> 转换为字符串

c++ - Global const string& 对我来说很难闻,它真的安全吗?

c++ - 为什么在引用(常量指针)可用时使用 const 关键字声明常量指针?

c++ - 如何在 C++ 类中设置数组

c++ - 3n+1 程序有什么问题?

c# - 从服务器读取二进制响应

c - 长字符串中的段错误

c++ - <string> 和 <string.h> 的区别?