forrest forrest - 2 years ago 74
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 Source

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

regex_iterator(BidirIt, BidirIt,
               const regex_type&&,
               std::regex_constants::match_flag_type) = delete;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download