Iterators

Feb 6, 2014 at 4:59pm
closed account (EwCjE3v7)
Will this point at the null character or ine past that

1
2
  string sv = "Hello";
  auto svend = sv.end();
Feb 6, 2014 at 5:13pm
A string doesn't have a null terminator. It knows where the string content begins and ends.

The null terminator is supplied for C compatibility in member c_str().

svend points to one past the end of the string.
Feb 6, 2014 at 5:30pm
In C++11 the std::string is null terminated but you are not allowed to dereference the end iterator.
Feb 6, 2014 at 5:44pm
@Peter87: to clarify, I think you mean that there has to be a null terminator in memory after the end of the string? This has always been the guarantee for .c_str() but not for .data(), could you link to somewhere explaining this difference or quote the standard?
Feb 6, 2014 at 5:48pm
closed account (EwCjE3v7)
Thank you so if I move svend back by one will it point to 'o'?
Feb 6, 2014 at 5:58pm
Yes
Feb 6, 2014 at 6:15pm
@LB Yes that is what I mean. In C++11 the specification is the same for both data() and c_str().

C++11 §21.4.7.1 basic_string accessors
const charT* c_str() const noexcept;
const charT* data() const noexcept;

Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
Complexity: constant time.
Requires: The program shall not alter any of the values stored in the character array.
Feb 6, 2014 at 8:39pm
closed account (EwCjE3v7)
Thank you guys, nooby here so yea. Thanks
Topic archived. No new replies allowed.