gpt4 book ai didi

c++ - 如何有效地为 `string_view` 的子字符串获取 `std::string`

转载 作者:IT老高 更新时间:2023-10-28 13:20:39 29 4
gpt4 key购买 nike

使用 http://en.cppreference.com/w/cpp/string/basic_string_view作为引用,我认为没有办法更优雅地做到这一点:

std::string s = "hello world!";
std::string_view v = s;
v = v.substr(6, 5); // "world"

更糟糕的是,这种幼稚的方法是一个陷阱,并让 v 成为对临时对象的悬空引用:

std::string s = "hello world!";
std::string_view v(s.substr(6, 5)); // OOPS!

似乎记得标准库中可能有一个附加项以将子字符串作为 View 返回:

auto v(s.substr_view(6, 5));

我可以想到以下解决方法:

std::string_view(s).substr(6, 5);
std::string_view(s.data()+6, 5);
// or even "worse":
std::string_view(s).remove_prefix(6).remove_suffix(1);

坦率地说,我认为这些都不是很好。现在我能想到的最好的事情就是使用别名来简单地让事情变得不那么冗长。

using sv = std::string_view;
sv(s).substr(6, 5);

最佳答案

有自由功能路线,但除非您还为 std::string 提供重载,否则它是一个蛇坑。

#include <string>
#include <string_view>

std::string_view sub_string(
std::string_view s,
std::size_t p,
std::size_t n = std::string_view::npos)
{
return s.substr(p, n);
}

int main()
{
using namespace std::literals;

auto source = "foobar"s;

// this is fine and elegant...
auto bar = sub_string(source, 3);

// but uh-oh...
bar = sub_string("foobar"s, 3);
}

恕我直言,string_view 的整个设计是一场恐怖表演,它将把我们带回到一个充满段错误和愤怒客户的世界。

更新:

即使为 std::string 添加重载也是一场恐怖表演。看看你是否能发现微妙的段错误定时炸弹......

#include <string>
#include <string_view>

std::string_view sub_string(std::string_view s,
std::size_t p,
std::size_t n = std::string_view::npos)
{
return s.substr(p, n);
}

std::string sub_string(std::string&& s,
std::size_t p,
std::size_t n = std::string::npos)
{
return s.substr(p, n);
}

std::string sub_string(std::string const& s,
std::size_t p,
std::size_t n = std::string::npos)
{
return s.substr(p, n);
}

int main()
{
using namespace std::literals;

auto source = "foobar"s;
auto bar = sub_string(std::string_view(source), 3);

// but uh-oh...
bar = sub_string("foobar"s, 3);
}

编译器在这里没有发现任何警告。我确信代码审查也不会。

我之前已经说过,我会再说一遍,以防 c++ 委员会的任何人在看,允许从 std::stringstd 的隐式转换::string_view 是一个可怕的错误,只会让 c++ 声名狼藉

更新

在 cpporg 留言板上提出了 string_view 的这个(对我来说)相当惊人的属性,我的担忧得到了冷漠。

这个小组的一致意见是 std::string_view 绝对不能从函数返回,这意味着我上面的第一个产品是错误的形式。

当然没有编译器可以帮助捕捉偶然发生的时间(例如通过模板扩展)。

因此,std::string_view 应该非常小心地使用,因为从内存管理的角度来看,它等同于指向另一个对象状态的可复制指针,它可能不再存在。但是,它在所有其他方面看起来和行为都像值类型。

因此代码如下:

auto s = get_something().get_suffix();

get_suffix() 返回 std::string 时是安全的(通过值或引用)

但如果 get_suffix() 曾被重构为返回 std::string_view,则为 UB。

在我看来,这意味着如果用户调用的库被重构为返回 std::string_view,任何使用 auto 存储返回字符串的用户代码都会中断代替 std::string const&.

所以从现在开始,至少对我来说,“几乎总是自动”必须变成“几乎总是自动,除非是字符串”。

关于c++ - 如何有效地为 `string_view` 的子字符串获取 `std::string`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46032307/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com