c++ - 直接写入 std::string 的 char* 缓冲区

标签 c++ string c++14 language-lawyer c++17

所以我有一个 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 any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().

所以 &str.front()&str[0]应该可以。

C++17

str.data() , &str.front()&str[0]工作。

Here它说:

charT* data() 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 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 type charT with value charT(), 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 and const_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/

相关文章:

c++ - 5 CPU的任务调度N进程

c++ - 有效地求和日志数量

Java - 如何创建子字符串直到到达非数字字符?

c++ - windows c++14 上的 clang-cl 状态

c++ - 在调用时仅指定一些模板参数

c++ - 二维数组在退出循环后移除其元素

c++ - Qml程序不运行

C++ 模板和派生类

c - scanf 拾取空格后的字符 C

Java 正则表达式为每个对象分割 JSON 字符串