- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
使用 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::string
到 std 的隐式转换::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/
假设我们有一个 string_view 和另一个 string_view,它是第一个 string_view 的子集: using namespace std; // just to shorten
我正在使用一个使用 boost::string_view 的 boost 库。但是,我想在我的代码中使用 std::string_view。 问:在这两者之间进行转换的最佳方式是什么? 目前我正在使用
std::string_view::remove_prefix() 和 std::string_view::remove_suffix() 都是 c 中的 constexpr 成员函数++17;但是,
我已经从 Bjarne Stroustrup 的 A Tour of C++ 中复制代码来测试字符串 View ,但我不断收到错误: error: no matching function for c
除了 std::string_view 方法之外,std::string_view 是否比 char* 有任何优势? 如果没有使用 string_view 的方法,是否有任何理由将 char* 重构为
我有这个循环: for (const std::string_view resource : resources) { ... } 由此资源被定义为 inline const std::string_
我有这个循环: for (const std::string_view resource : resources) { ... } 由此资源被定义为 inline const std::string_
而一个 span可以从一个范围构建,一个 string_view不能从一系列字符构造。 因此,例如,需要以下代码: // assume chars_span is a span of chars st
void Foo1(string_view view) { ... } string str = "one two three"; Foo1("one two three"); // Implicit
我有一个字符串,其中包含用 , 字符分隔的数字序列。为了将序列中的值读入数组,我创建了以下 GCC 10 拒绝编译的代码: #include #include #include #include
我在使用 Boost 1.70 时发现了一个相当令人费解的问题,有时 boost::string_view 似乎指向另一个字符串。 这是创建 boost::string_view 的函数调用,如下所示
在以下场景中使用 std::string_view: struct A : public std::exception{ A (const char* c) : v_(c){} con
我正在尝试尽可能多地使用 std::string_view 来包装 C 字符串,但是,每当我正在包装的 C 字符串被动态分配时,我都依赖于此图案: char *cs = get_c_string();
我读了The most elegant way to iterate the words of a string并享受答案的简洁性。现在我想对 string_view 做同样的事情。问题是,strin
我有以下代码: #include class Foo { public: Foo(std::string_view) {} }; 当我这样做时,一切都可以正常编译(使用 clang v8,C
根据一篇文章(here 和 there),这段代码是一个错误的免费示例: #include #include #include int main() { std::string s = "H
string_view 是 C++ 库基础 TS(N3921) 中的一项提议功能,添加到 C++17 据我所知,它是一种代表某种字符串“概念”的类型,它是任何类型的容器的 View ,可以将可视内容存
考虑以下代码: #include #include int main() { std::optional opt { "abc" }; std::cout { std::stri
在 std::string_view 上匹配正则表达式工作正常。但是当我返回匹配的子字符串时,它们会因为某种原因而消失。 std::string_view 参数在函数作用域结束时被销毁,但它指向的内存
This reference指出第二个示例产生了一个悬空指针。如何在第二个表达式中创建悬挂指针而不是在第一个表达式中? std::string_view good("a string literal"
我是一名优秀的程序员,十分优秀!