Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

C++ agrees with your "opponent":

#include <iostream>
#include <string>
using namespace std;

int main()
{
    bool contains = string("").find(string("")) != string::npos;
    cout << "\"\" contains \"\": "
        << boolalpha << contains;
}

Output: "" contains "": true

Demo

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.

The first thing that is unclear is whether you are talking about std::string or null terminated C strings, the second thing is why should it matter?. I will assume std::string.

The requirements on std::string determine how the component must behave, not what its internal representation must be (although some of the requirements affect the internal representation). As long as the requirements for the component are met, whether it holds something internally is an implementation detail that you might not even be able to test.

In the particular case of an empty string, there is nothing that mandates that it holds anything. It could just hold a size member set to 0 and a pointer (for the dynamically allocated memory if/when not empty) also set to 0. The requirement in operator[] requires that it returns a reference to a character with value 0, but since that character cannot be modified without causing undefined behavior, and since strict aliasing rules allow reading from an lvalue of char type, the implementation could just return a reference to one of the bytes in the size member (all set to 0) in the case of an empty string.

Some implementations of std::string use small object optimizations, in those implementations there will be memory reserved for small strings, including an empty string. While the std::string will obviously not contain a std::string internally, it might contain the sequence of characters that compose an empty string (i.e. a terminating null character)

empty string doesn't contain anything - it's EMPTY. :)

Of course an empty string does not contain an empty string. It'll be turtles all the way down if it did.

Take String empty = ""; that is declaring a string literal that is empty, if you want a string literal to represent a string literal that is empty you would need String representsEMpty = """"; but of course, you need to escape it, giving you string actuallyRepresentsEmpty = "\"\"";

ps, I am taking a pragmatic approach to this. Leave the maths nonsense at the door.

Thinking about you amendment, it could be possible that your 'opponent' meant was that an 'empty' std::string still has an internal storage for characters which is itself empty of characters. That would be an implementation detail I am sure, it could perhaps just keep a certain size (say 10) array of characters 'just incase', so it will technically not be empty.

Of course, there is the trick question answer that 'nothing' fits into anything infinite times, a sort of 'divide by zero' situation.

Today I had the same question since I'm currently bound to a lousy STL implementation (dating back to the pre-C++98 era) that differs from C++98 and all following standards:

TEST_ASSERT(std::string().find(std::string()) == string::npos); // WRONG!!! (non-standard)

This is especially bad if you try to write portable code because it's so hard to prove that no feature depends on that behaviour. Sadly in my case that's actually true: it does string processing to shorten phone numbers input depending on a subscriber line spec.

On Cppreference, I see in std::basic_string::find an explicit description about empty strings that I think matches exactly the case in question:

an empty substring is found at pos if and only if pos <= size()

The referred pos defines the position where to start the search, it defaults to 0 (the beginning).

A standard-compliant C++ Standard Library will pass the following tests:

TEST_ASSERT(std::string().find(std::string()) == 0);
TEST_ASSERT(std::string().substr(0, 0).empty());
TEST_ASSERT(std::string().substr().empty());

This interpretation of "contain" answers the question with yes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top