所以我有一个 std::string
和一个接受 char*
并写入其中的函数。由于 std::string::c_str()
和 std::string::data()
返回 const char*
,我不能使用它们。所以我正在分配一个临时缓冲区,用它调用一个函数并将它复制到 std::string
。
现在我计划处理大量信息,复制此缓冲区会产生明显的影响,我想避免它。
有些人建议使用 &str.front()
或 &str[0]
但它会调用未定义的行为吗?
最佳答案
C++98/03
不可能。字符串可以在写入时复制,因此它需要处理所有读取和写入。
C++11/14
在 [string.require] 中:
The char-like objects in a
basic_string
object shall be stored contiguously. That is, for anybasic_string
objects
, the identity&*(s.begin() + n) == &*s.begin() + n
shall hold for all values ofn
such that0 <= n < s.size()
.
所以 &str.front()
和 &str[0]
应该可以。
C++17
str.data()
, &str.front()
和 &str[0]
工作。
Here它说:
charT* data() noexcept;
Returns: A pointer
p
such thatp + i == &operator[](i)
for eachi
in[0, size()]
.Complexity: Constant time.
Requires: The program shall not alter the value stored
at p + size()
.
非常量 .data()
刚刚好。
recent draft .front()
有以下措辞:
const charT& front() const;
charT& front();
Requires:
!empty()
.Effects: Equivalent to
operator[](0)
.
下面是 operator[]
:
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 typecharT
with valuecharT()
, where modifying the object leads to undefined behavior.Throws: Nothing.
Complexity: Constant time.
所以它使用迭代器算法。所以我们需要检查有关迭代器的信息。 Here它说:
3 A basic_string is a contiguous container ([container.requirements.general]).
所以我们需要去here :
A contiguous container is a container that supports random access iterators ([random.access.iterators]) and whose member types
iterator
andconst_iterator
are contiguous iterators ([iterator.requirements.general]).
然后 here :
Iterators that further satisfy the requirement that, for integral values n and dereferenceable iterator values
a
and(a + n)
,*(a + n)
is equivalent to*(addressof(*a) + n)
, are called contiguous iterators.
显然,连续迭代器是一个 C++17 特性,它是在 these 中添加的。 papers .
需求可以重写为:
assert(*(a + n) == *(&*a + n));
因此,在第二部分中,我们取消引用迭代器,然后获取它指向的值的地址,然后对其进行指针运算,取消引用它,这与递增迭代器然后取消引用它相同。这意味着连续迭代器指向每个值紧接着另一个存储的内存,因此是连续的。由于采用 char*
的函数期望连续内存,您可以传递 &str.front()
的结果或 &str[0]
这些功能。
关于c++ - 直接写入 std::string 的 char* 缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200665/