我想將一串整數對解析爲數字。我用這個代碼:將臨時std :: string傳遞給boost :: regex_match
#include <iostream>
#include <boost/regex.hpp>
int main()
{
boost::regex reg("(\\d+):(\\d+)");
std::string test = "1:2 3:4 5:6";
boost::sregex_token_iterator end;
for(boost::sregex_token_iterator i(test.begin(), test.end(), reg); i != end; ++i) {
boost::smatch what;
if(boost::regex_match(i->str(), what, reg))
std::cout << "found: \"" << what[1].str() << "\":\"" << what[2].str() << "\"" << std::endl;
}
return 0;
}
預期輸出:
found: "1":"2"
found: "3":"4"
found: "5":"6"
我與GCC 4.7.2編譯提升1.52得到了什麼:
found: "2":"2"
found: "4":"4"
found: "6":"6"
提升1.52鐺3.2:
found: "":"2"
found: "":"4"
found: "":"6"
我的代碼有什麼問題?
值得一提的是,在clang ++/libC++中實現的'std :: regex'輸出期望的輸出。 – Cubbi 2013-03-05 01:59:39
@Cubbi它仍然可以是UB,除非它們改變了smatch如何存儲子表達式的結果 – Slava 2013-03-05 02:03:05