问题标签 [string-view]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
308 浏览

c++ - 是否扩展了 string_view 未定义的行为?

假设我们在 s 的某些部分有字符串 s 和 string_view sv 使得

换句话说,sv 结尾之后的字符仍然是 s 的一部分。以下是定义的还是未定义的行为?

那么我们可以扩展一个 string_view 吗?

来自 cppreference:

如果 [s, s+count) 不是有效范围,则行为未定义(即使构造函数可能无法访问此范围的任何元素)。

[sv.data(), sv.data() + sv.size() + 1) 是有效范围,因为它是 s 的一部分吗?

0 投票
2 回答
362 浏览

c++ - 在只读场景中何时需要以空字符结尾的字符串?

我一直在玩这个std::string_view库,并且一直在考虑更改我一直在努力的代码库以std::string_view尽可能多地使用。但是,在我读过的关于何时何地使用std::string_view而不是const std::string &. 我看到很多答案说,“当你不需要一个空终止的字符串时。” 所以当我开始在网上搜索时,“你什么时候需要一个以空字符结尾的字符串?” 我还没有真正遇到关于这个主题的任何有用的答案。

我可以想到一个外部库的示例,您将链接到它需要一个std::string. 在这种情况下,您将需要一个以空字符结尾的字符串,因为该库需要它。我想另一个例子是如果你需要修改字符串本身,但是const &如果我们需要修改它,我们就不会通过它。

那么什么时候需要使用以空字符结尾的字符串呢?

我看过的链接:

  1. std::string_view 到底比 const std::string& 快多少?
  2. 我什么时候会通过 const& std::string 而不是 std::string_view?
  3. 为什么只有字符串视图?
  4. 在 C++17 中使用 const std::string& 参数是否有意义?
0 投票
1 回答
296 浏览

c++ - 为什么没有string_view的演绎指南?

在查看 的引用时basic_string_view,似乎缺乏(显式)用于构造 from std::basic_string- 是否一致的推导指南,似乎为指针类型(const char*const wchar_t*)生成了隐式推导指南

目前,我在模板中采用以下技巧,该模板应该只接受可以粘贴到字符串视图上的任何内容:

我宁愿只写:

我想知道这是否被考虑过?

0 投票
0 回答
171 浏览

c++ - 这是用“std::string_view”或“std::string”替换“const std::string &”的好习惯吗?

const std::string &当我需要和 s 一起玩时,我总是更喜欢std::string。但是最近,我突然发现根本没有必要使用const std::string &。对于std::string_view只读字符串会更好,而std::string对于move其他情况(设置字符串字段等)会更好。

以上两种情况,const std::string &都不是最优的!

所以我决定删除所有const std::string &s 并用其中一个替换它们。所以我的问题是:

  1. 会有const std::string &更好的情况吗?
  2. 如果没有,我还需要const std::string &吗?

谢谢你。

0 投票
3 回答
19518 浏览

c++ - How to correctly create std::string from a std::string_view?

I have a class:

and there is a lib-func that I can't modify:

If there is a local variable:

When I need to call loadData, I dare not do it like this:

I have to do like this:

My question: Is the first method correct? or I must use the second one?

Because I think that in Method 1, the iterators I passed to the constructor of std::string, are of two Different string_vew objects, and theoretically the result is undefined, even though we would get expected result with almost all of the C++ compilers.

Any hints will be appreciated! thanks.

0 投票
1 回答
538 浏览

c++ - 如何在编译时从 string_view 中删除子字符串?

我创建了一个名为 DBG 的宏,它打印一个表达式本身和它计算的值。所以DBG(5+1)应该打印5+1 = 6。该宏工作正常。

然而,如果我封装了多个这些宏,那将变得非常不可读,因为“DBG”本身总是被拖来拖去。

我想要做的是在编译时从表达式本身中删除所有出现的子字符串“DBG”。这样DBG(DBG(5*3) + DBG(20/4))结果就不会

但反而

如果需要:宏看起来像这样:#define DBG(expression) debug_log((#expression, expression)debug_log 是:

我已经编写了一个应该执行此操作的辅助函数,但我不知道如何在编译时连接两个 string_views。

0 投票
2 回答
572 浏览

c++ - 将字符串存储在 constexpr 结构中

是否可以将字符串存储在constexpr结构中:

到目前为止,我只能想出:

如果像这样使用此类,这显然是一个好主意

而不是这样

我需要constexpr在编译时构造结构。是否有可能在运行时使其更安全,因为std::string_view它不是。

0 投票
3 回答
2227 浏览

c++ - 为什么 std::string_view 比 const char* 快?

还是我在测量其他东西?

在这段代码中,我有一堆标签(integers)。每个标签都有一个字符串表示(const char*std::string_view)。在循环堆栈值被转换为相应的字符串值。这些值附加到预先分配的字符串或分配给数组元素。

结果表明,带有 的版本std::string_view略快于带有 的版本const char*

代码:

我的机器上的结果是:

Godbolt 编译器浏览器网址:https ://godbolt.org/z/SMrevx

UPD:更准确的基准测试后的结果(500 次运行 300000 次迭代):

神螺栓网址: https ://godbolt.org/z/aU7zL_

所以在第二种情况下const char*比预期的更快。答案中解释了第一种情况。

0 投票
1 回答
109 浏览

c++ - 从 const std::string& 移动到 std::string_view 时的向后兼容性考虑?

const std::string&我维护了一个经常在其 API 中使用参数的 C++ 库。但是,我收到了一些用户请求切换到std::string_view以帮助实现当前 API 无法实现的效率。

我正在考虑简单地将所有const std::string&参数实例替换为std::string_view(可能使用验证std::string_view可用的功能检查)。这会破坏我的任何用户的向后兼容性吗?我尝试了简单的替换,它似乎没有破坏我的代码或测试中的任何内容,但这当然不是一个详尽的检查。

我确实意识到这会破坏一些取决于我的库的确切函数签名的代码。为了简单起见,假设我不允许用户依赖于我的函数的确切类型签名/参数。

0 投票
1 回答
806 浏览

c++ - 将 std::string_view 返回到 const char *

因此,我在这里阅读了using enum作为新 C++2a 标准的一部分的内容,并遇到了以下代码:

这让我想知道返回 std::string_view 是如何工作的?在我看来“红色”,“绿色”,......是临时变量,因此不应该工作,所以我写了一些测试代码:

输出:

蓝色和 alpha 的行为符合我的预期(未定义行为),但红色和绿色没有。所以,我的问题是为什么const char*似乎有效?或者这只是未定义行为的 msvc 表现?