banner
cells

cells

为美好的世界献上 code

Q284

Q284#

#include <iostream>
#include <string>

auto main() -> int {
    std::string out {"Hello world"};
    std::cout << (out[out.size()] == '\0');
}

Answer#

click to see answer 1

Explain#

const_reference operator[](size_type pos) const;
reference operator[](size_type pos);

Requires: pos <= size().

Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.

charT() is char() in this case. The char() expression value-initializes a char, which initializes it to 0.

So out[out.size()] == '\0', 0 = '\0'

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.