c++ - Xml Parser - string::find -
i trying parse string contains line of xml file.
std::string temp = "<album>underclass hero</album>"; int f = temp.find(">"); int l = temp.find("</"); std::string _line = temp.substr(f + 1, l-2);
this part of code of function should return parsed string. expected returns underclass hero. instead got underclass hero< /alb
(here between '<' , '/' space because couldn't write them together).
i looked std::string::find several times , said returns, if existing, position of first character of first match. here gives me last character of string, in variable l.
f fine.
so can tell me i'm doing wrong?
the second argument takes length of substring want extract. can fix code way:
#include <string> #include <iostream> int main() { std::string temp = "<album>underclass hero</album>"; int f = temp.find(">"); int l = temp.find("</"); std::string line = temp.substr(f + 1, l - f - 1); // ^^^^^^^^^ }
here live example.
also, careful names such _line
. per paragraph 17.6.4.3.2/1 of c++11 standard:
[...] each name begins underscore reserved implementation use name in global namespace.
Comments
Post a Comment