开发者

std::string::find_first_of does not return the expected value

开发者 https://www.devze.com 2023-04-11 20:37 出处:网络
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:

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  
(gd开发者_如何学Gob) 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消