草庐IT

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

coder 2024-02-01 原文

这个问题来自于 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++ - 字符串是否到处都包含空子字符串?的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  3. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  4. ruby-on-rails - unicode 字符串的长度 - 2

    在我的Rails(2.3,Ruby1.8.7)应用程序中,我需要将字符串截断到一定长度。该字符串是unicode,在控制台中运行测试时,例如'א'.length,我意识到返回了双倍长度。我想要一个与编码无关的长度,以便对unicode字符串或latin1编码字符串进行相同的截断。我已经了解了Ruby的大部分unicode资料,但仍然有些一头雾水。应该如何解决这个问题? 最佳答案 Rails有一个返回多字节字符的mb_chars方法。试试unicode_string.mb_chars.slice(0,50)

  5. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  6. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  7. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  8. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  9. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  10. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

随机推荐