c++ - Boost::string_ref 查找算法

标签 c++ string boost

我在弄清楚 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/

相关文章:

c++ - llvm-ld 仍然存在于 clang 3.4 吗?

c# - 比较字符串是否相等

c++ - boost 区域设置格式百分比

c++ - OpenCV 和网络摄像机 - 或者 - 如何监视邻居?

c++ - 如果我有一条从点 x1、y1 到 x2、y2 的线,我怎样才能从点 x2、y2 以 45 度角绘制 2 条线来制作箭头?

python - 获取 IndexError : string index out of range in python

c++ - Visual Studio 链接器找不到 libboost_system

c++ - Boost C++ http post 获取服务 : How to make more than one run on same port?

c++ - Lambda 函数 - 按值捕获

c++ - 在 vector<string> 中存储两个间隔字符串