为什么string :: data()不提供Mutable char *? [英] Why Doesn't string::data() Provide a Mutable char*?

查看:98
本文介绍了为什么string :: data()不提供Mutable char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++ 11 array string vector 全部得到了 data 方法,该方法为:

In c++11 array, string, and vector all got the data method which:


返回指向用作元素的基础数组的指针存储。指针的范围是[ data();即使容器为空, data()+ size())始终是有效范围。
[]

Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(); data() + size()) is always a valid range, even if the container is empty. [Source]

此方法以可变的 const 版本提供,适用于所有适用的容器,例如:

This method is provided in a mutable and const version for all applicable containers, for example:

T* vector<T>::data();
const T* vector<T>::data() const;

所有适用的容器,即 string 仅提供 const 版本

All applicable containers, that is, except string which only provides the const version:

const char* string::data() const;

这里发生了什么?为什么 string 变短了,而 char * string :: data()会如此有用?

What happened here? Why did string get shortchanged, when char* string::data() would be so helpful?

推荐答案

简短的答案是确实提供了 char * string :: data()方法。对于类似的 c + +17 data 函数,这样就可以对底层C字符串进行可变访问:

The short answer is that c++17 does provide the char* string::data() method. Which is vital for the similarly c++17 data function, thus to gain mutable access to the underlying C-String I can now do this:

auto foo = "lorem ipsum"s;

for(auto i = data(foo); *i != '\0'; ++i) ++(*i);






出于历史目的,值得编年表 string 的发展,其中建立在以下基础上:在 string 的基础缓冲区的访问因其元素的新要求而成为可能连续存储,这样对于任何给定的 string s


For historical purposes it's worth chronicling string's development which c++17 is building upon: In c++11 access to string's underlying buffer is made possible possible by a new requirement that it's elements are stored contiguously such that for any given string s:


& *(s.begin()+ n)==& * s.begin()+ n 对于任何 n [ 0 s.size()),或等效地指向 s的指针[0] 可以传递给期望指向 CharT [] 数组第一个元素的指针的函数。

&*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of a CharT[] array.

可以通过v获得对该新要求的基础C字符串的可变访问各种方法,例如:& s.front()& s [0] & * s.first()但是回到最初的问题,它将避免使用以下选项之一的负担:为什么无法访问 string 的基础缓冲区是以 char * string :: data()

Mutable access to this newly required underlying C-String was obtainable by various methods, for example: &s.front(), &s[0], or &*s.first() But back to the original question which would avoid the burden of using one of these options: Why hasn't access to string's underlying buffer been provided in the form of char* string::data()?

要回答这一点,必须注意 T * array< T> :: data() T * vector< T> :: data()的问题。 string 是连续的。在此之前 const char * string :: data() 已经存在。尽管明确保证要指向任何底层缓冲区,但这是从<$ c中获得 const char * 的唯一方法$ c> string

To answer that it is important to note that T* array<T>::data() and T* vector<T>::data() were an addition required by c++11. No additional requirements were incurred by c++11 against other contiguous containers such as deque. And there certainly wasn't an additional requirement for string, in fact the requirement that string was contiguous was new to c++11. Before this const char* string::data() had existed. Though it explicitly was not guaranteed to be pointing to any underlying buffer, it was the only way to obtain a const char* from a string:


返回的数组不需要以空值结尾。

The returned array is not required to be null-terminated.

这意味着 string 并未变短 agged / c%2b%2b11 class = post-tag title =显示已标记'c ++ 11'的问题 rel = tag> c ++ 11 过渡到 data 访问器,因此根本不包括它,因此只有 const data 访问者先前拥有的 string 仍然存在。在C ++ 11的实现中,有自然发生的示例,这需要直接写入字符串的基础缓冲区。

This means that string was not "shortchanged" in c++11's transition to data accessors, it simply was not included thus only the const data accesor that string previously possessed persisted. There are naturally occurring examples in C++11's implementation which necessitate writing directly to the underlying buffer of a string.

这篇关于为什么string :: data()不提供Mutable char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆