问题描述
在 array
, string
和 vector
全部得到了 data
方法,该方法为:
In c++11 array
, string
, and vector
all got the data
method which:
此方法以可变的 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 :
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?
推荐答案
简短的答案是确实提供了。对于类似的 ,这样就可以对底层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
:
可以通过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()
是的问题。 是。在此之前已经存在。尽管不明确保证要指向任何底层缓冲区,但这是从<$ 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
:
这意味着 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 *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!