我在弄清楚 boost 库 string_ref 查找函数的作用时遇到了一些问题。我写了下面的程序
int main(int argc, char **argv) {
boost::string_ref ref = string("<a>\n1234567\n</a>\n<a>");
cout << ref.find_first_of("<a>") << endl; // output 12
cout << ref.find("<a>") << endl; // output 17
}
我不明白为什么 find 给出了我模式的最后一次出现,而 find_last_of 甚至根本没有意义!有人可以帮我吗?
最佳答案
来自 docs :
Boost.StringRef is an implementation of Jeffrey Yaskin's N3442: string_ref: a non-owning reference to a string.
string_ref
不拥有任何东西,只是引用它。所以当你写:
boost::string_ref ref = string("<a>\n1234567\n</a>\n<a>");
那个临时 string
在行尾被销毁,现在你有一个悬空引用。其他一切都是UB。
关于c++ - Boost::string_ref 查找算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39751776/