Does an empty string contain an empty string in C++?

后端 未结 7 1370
渐次进展
渐次进展 2021-01-03 00:53

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

相关标签:
7条回答
  • 2021-01-03 01:49

    It's easy. String A contains sub-string B if there is an argument offset such that A.substr(offset, B.size()) == B. No special cases for empty strings needed.

    So, let's see. std::string("").substr(0,0) turns out to be std::string(""). And we can even check your "counter-example". std::string("").substr(0,0).substr(0,0) is also well-defined and empty. Turtles all the way down.

    0 讨论(0)
提交回复
热议问题