Is C ++ 11 std :: string a basic representation guaranteed to have a null termination character?

Some excerpts from the standard first:

Spec for string::operator[]():

const_reference operator [] (size_type pos) const;
reference operator [] (size_type pos);
Requires : pos <= size ().
Returns : * (begin () + pos) if pos <size (), otherwise a reference to an object of type T with value charT (); the reference value should not be modified.
Difficulty : constant time.

Spec for string::c_str()and string::data():

const charT * c_str () const noexcept;
const charT * data () const noexcept;
Returns : a pointer p such that p + i == &operator[](i)for each I am in [0, size ()].
Complexity : constant time.

2 , , p, c_str()/data(), , p[0...size()-1] , p[size()] operator[](size()), charT(). charT(), ( charT() ). , ++ 11 , str::string ?


. , , . , , , .

, : std::string charT() c_str()/data() . (.. size()+1 .)

+4
3

, . std::string - c_str() data(). , , .

.

+2

, ++ 11 , std::string ?

, . , , , , c_str() data().

+1

No ... It is safe to say that he says: operator[](size()) == CharT()and c_str()[size()] == 0and data()[size()] == 0.

Regardless of whether a "basic storage," the value 0 in it, certain implementations, and should not affect your program, because there is no access to it, except with the help of elements, such as operator[], and c_str()that are well defined for size().

+1
source

Source: https://habr.com/ru/post/1531176/


All Articles