forrest forrest - 3 months ago 10
C++ Question

Why do I get two different results while using sregex_iterator with a regex variable pattern vs a constructed pattern

If I input "110110" in to the application, the first chunk prints out "1" while the second chunk prints out "2".

Why are the results different?

How can I construct the regex pattern in the function call?

#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;

auto begin = sregex_iterator(s.begin(),s.end(),regex{R"(110)"});
auto end = sregex_iterator();
cout << distance(begin,end) << endl;

const regex r(R"(110)");
begin = sregex_iterator(s.begin(),s.end(),r);
end = sregex_iterator();
cout << distance(begin,end) << endl;
}

Answer

C++14 fixed the interface by deleting the constructor:

regex_iterator(BidirIt, BidirIt,
               const regex_type&&,
               std::regex_constants::match_flag_type) = delete;