在不复制的情况下为字符串使用外部缓冲区(Use an external buffer for a string without copying)

编程入门 行业动态 更新时间:2024-10-02 14:23:17
在不复制的情况下为字符串使用外部缓冲区(Use an external buffer for a string without copying)

假设我有一个函数获取const string&作为其输入,例如:

void foo(const string& s);

然后我有一个内部缓冲区const char* buffer; 我知道它的大小。

我认为,如果我创建了内联字符串,仍然会发生一个副本:

foo(string(buffer, n));

但是没有必要复制缓冲区,因为所有的东西都是常量,我只需要字符串类的功能而不是它创建的缓冲区。

我必须提到,我不确定复制是否发生,但是看看字符串的构造函数 ,他们都说复制会发生。 我不知道编译器优化可以理解这种情况或不能,我找不到确保副本是否发生的方法。

有什么办法可以为字符串使用外部缓冲区,或者至少有一种方法来确保复制是否发生。 我目前使用std string和c ++ 11。

Assume that I have a function that gets const string& as its input for example:

void foo(const string& s);

And then I have an internal buffer const char* buffer; which I know the size of it.

I think if I create string inline, still one copy would happen:

foo(string(buffer, n));

But there is no need to copy the buffer because all things are constant and I just need the functionality of string class not the buffer that it creates.

I must mention that I am not sure that copy happens or not, But looking at the constructor of string all of them said that copy would happen. I don't know compiler optimization can understand such situations or not and I could not find a way to sure that copy happened or not.

Is there any way to use an external buffer for string, or at least a way to sure that copy happens or not. I am using std string and c++11 currently.

最满意答案

是的,复制总是在发生。 顺便说一句,你不需要包装std::string(buffer)作为构造函数std::string(char const*)是隐式的,而且简单

foo(buffer);

会隐式地将缓冲区复制到字符串中。 如果你是foo的作者,你可以添加一个重载

void foo(char const*)

避免复制。 但是,C字符串遇到了null终止符是字符串API的一部分的问题,因此如果不更改基础字符串(la strtok ),就不能轻松创建子字符串。

库基础技术规范包含一个string_view类,它将消除类似char const*的拷贝,但保留std::string的子集容量

#include <iostream> #include <experimental/string_view> void foo(std::experimental::string_view v) { std::cout << v.substr(2,8) << '\n'; } int main() { char const* buffer = "war and peace"; foo(buffer); }

实例 (在C ++ 14模式下需要libstdc ++ 4.9或更高版本)。

Yes, copying is always happening. BTW, you don't need to wrap std::string(buffer) as the constructor std::string(char const*) is implicit and a simple

foo(buffer);

will implicitly copy the buffer into the string. If you are the author of foo you can add an overload

void foo(char const*)

that avoids the copying. However, C strings are suffering from the problem that the null terminator is part of the string APIs, and so you can't easily create substrings without mutating the underlying string (a la strtok).

The Library Fundamentals Technical Specification contains a string_view class that will eliminate the copying like char const*, but preserves the subset capability of std::string

#include <iostream> #include <experimental/string_view> void foo(std::experimental::string_view v) { std::cout << v.substr(2,8) << '\n'; } int main() { char const* buffer = "war and peace"; foo(buffer); }

Live Example (requires libstdc++ 4.9 or higher in C++14 mode).

更多推荐

本文发布于:2023-08-04 21:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓冲区   字符串   情况下   external   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!