C++ 什么是 string_view?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20803826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:19:41  来源:igfitidea点击:

What is string_view?

c++c++17string-viewfundamentals-ts

提问by Drax

string_viewwas a proposed feature within the C++ Library Fundamentals TS(N3921) added to C++17

string_view是添加到 C++17的 C++ 库基础 TS(N3921)中的一项提议特性

As far as i understand it is a type that represent some kind of string "concept" that is a view of any type of container that could store something viewable as a string.

据我所知,它是一种代表某种字符串“概念”的类型,它是任何类型的容器的视图,可以存储可作为字符串查看的内容。

  • Is this right ?
  • Should the canonical const std::string&parameter type become string_view?
  • Is there another important point about string_viewto take into consideration ?
  • 这是正确的吗 ?
  • 规范const std::string&参数类型应该 变成string_view?
  • 是否还有另一个重要的问题string_view需要考虑?

回答by Kerrek SB

The purpose of any and all kinds of "string reference" and "array reference" proposals is to avoid copying data which is already owned somewhere else and of which only a non-mutating view is required. The string_viewin question is one such proposal; there were earlier ones called string_refand array_ref, too.

任何和所有类型的“字符串引用”和“数组引用”提议的目的是避免复制已经在其他地方拥有的数据并且只需要非变异视图。该string_view问题是这样的一个建议; 也有更早的调用string_refand array_ref

The idea is always to store a pair of pointer-to-first-element and size of some existingdata array or string.

这个想法总是存储一对指向第一个元素的指针和一些现有数据数组或字符串的大小。

Such a view-handle class could be passed around cheaply by value and would offer cheap substringing operations (which can be implemented as simple pointer increments and size adjustments).

这样的视图句柄类可以通过值廉价地传递,并提供廉价的子串操作(可以实现为简单的指针增量和大小调整)。

Many uses of strings don't require actual owning of the strings, and the string in question will often already be owned by someone else. So there is a genuine potential for increasing the efficiency by avoiding unneeded copies (think of all the allocations and exceptions you can save).

字符串的许多用途不需要实际拥有这些字符串,并且所讨论的字符串通常已经为其他人所有。因此,通过避免不需要的副本(想想您可以保存的所有分配和异常),确实有提高效率的潜力。

The original C strings were suffering from the problem that the null terminator was part of the string APIs, and so you couldn't easily create substrings without mutating the underlying string (a la strtok). In C++, this is easily solved by storing the length separately and wrapping the pointer and the size into one class.

原始的 C 字符串遇到的问题是空终止符是字符串 API 的一部分,因此您无法在不改变底层字符串 (a la strtok) 的情况下轻松创建子字符串。在 C++ 中,这可以通过单独存储长度并将指针和大小包装到一个类中来轻松解决。

The one major obstacle and divergence from the C++ standard library philosophy that I can think of is that such "referential view" classes have completely different ownership semantics from the rest of the standard library. Basically, everything else in the standard library is unconditionally safe and correct (if it compiles, it's correct). With reference classes like this, that's no longer true. The correctness of your program depends on the ambient code that uses these classes. So that's harder to check and to teach.

我能想到的与 C++ 标准库哲学的一个主要障碍和分歧是,这种“引用视图”类与标准库的其余部分具有完全不同的所有权语义。基本上,标准库中的其他所有内容都是无条件安全和正确的(如果编译,则是正确的)。有了这样的参考类,这不再是真的。程序的正确性取决于使用这些类的环境代码。所以这更难检查和教导。