std::string::find_first_of 未返回预期值
我正在尝试用 C++ 创建 XML 解析器。我目前使用 cygwin 和 gcc 进行编译,使用 gdb 进行调试。我有这段代码:
const size_t mDataSize = mData.size();
...
size_t ltPos = mData.find_first_of('<', pos);
if (ltPos==mData.npos) {
...
mData 被声明为 private const std::string &在类中并保存 XML 文件内容。使用 gdb 进行调试后,我发现以下内容:
(gdb) print pos
$12 = 636
(gdb) print mDataSize
$13 = 2692
(gdb) n
141 size_t ltPos = mData.find_first_of('<', pos);
(gdb) print ltPos
$14 = 114
(gdb) print pos
$15 = 636
(gdb) n
143 if (ltPos==mData.npos)
(gdb) print ltPos
$16 = 4294967295
(gdb) print mData[636]
$17 = (const char &) @0xb2b2a8: 10 '\n'
(gdb) print mData[637]
$18 = (const char &) @0xb2b2a9: 32 ' '
(gdb) print mData[638]
$19 = (const char &) @0xb2b2aa: 32 ' '
(gdb) print mData[639]
$20 = (const char &) @0xb2b2ab: 60 '<'
我期望调用 find_first_of 得到 639,但我得到 4294967295(在有符号的 32 位 int 中为 -1 并与 std::string::npos 匹配)。有人可以证明这种行为合理吗?或者告诉我如何解决这个问题?
I am trying to create an XML parser in C++. I am currently using cygwin and gcc to compile and gdb to debug. I have this piece of code:
const size_t mDataSize = mData.size();
...
size_t ltPos = mData.find_first_of('<', pos);
if (ltPos==mData.npos) {
...
mData is declared as private const std::string & within the class and holds the XML file content. After debugging with gdb I found the following:
(gdb) print pos
$12 = 636
(gdb) print mDataSize
$13 = 2692
(gdb) n
141 size_t ltPos = mData.find_first_of('<', pos);
(gdb) print ltPos
$14 = 114
(gdb) print pos
$15 = 636
(gdb) n
143 if (ltPos==mData.npos)
(gdb) print ltPos
$16 = 4294967295
(gdb) print mData[636]
$17 = (const char &) @0xb2b2a8: 10 '\n'
(gdb) print mData[637]
$18 = (const char &) @0xb2b2a9: 32 ' '
(gdb) print mData[638]
$19 = (const char &) @0xb2b2aa: 32 ' '
(gdb) print mData[639]
$20 = (const char &) @0xb2b2ab: 60 '<'
I was expecting 639 as result of calling find_first_of, but I am getting 4294967295 (which is -1 in a signed 32-bit int and matches std::string::npos). Can someone justify this behaviour? Or tell me how to workaround this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么 mData 被声明为引用?如果是这样,它并不真正保存内容,而是保存对内容的引用。当您调用 find_first_of 时,mData 引用的内容是否仍然存在?
So mData is declared as a reference? If so it doesn't really hold the content it holds a reference to the content. Is the thing to which mData refers still in existence at the time you're calling find_first_of?