Boost::string_ref find algorithms

Boost::string_ref find algorithms

本文关键字:find algorithms ref string Boost      更新时间:2023-10-16

我有一些问题弄清楚什么是boost库string_reffind函数做。我写了下面的程序

int main(int argc, char **argv) {
    boost::string_ref ref = string("<a>n1234567n</a>n<a>");
    cout << ref.find_first_of("<a>") << endl; // output 12
    cout << ref.find("<a>") << endl; // output 17
}

我不明白为什么find给了我模式的最后一次出现,而find_last_of甚至根本没有意义!有人能帮帮我吗?

From the docs:

提振。StringRef是Jeffrey Yaskin的N3442的实现:string_ref: a 对字符串的非所属引用

string_ref不拥有任何东西,只是指代它。所以当你写:

boost::string_ref ref = string("<a>n1234567n</a>n<a>");

,临时string在行尾被销毁,现在你有一个悬空引用。其他都是UB。