#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
#define debug(exp) do { cout << #exp << ": " << (exp) << endl; } while (0)
int main (int argc, char *argv[])
{
vector<string> res;
string pattern (""),
text ("ilovechina");
vector<string> dict = { "i", "love", "china", "lovechina", "ilove" };
vector<string>::iterator dict_it = dict.begin ();
if (dict_it == dict.end ()) throw invalid_argument (__func__);
while (true)
{
pattern += *dict_it;
if (++dict_it == dict.end ()) break;
else pattern += "|";
}
debug (pattern);
regex re (pattern);
sregex_iterator ri (text.begin (), text.end (), re),
ri_end;
for (; ri != ri_end; ++ri)
{
ssub_match sm = (*ri)[0];
if (ri->position (0)) // The near string does not correspond to the dictionary
{
debug (ri->position (0));
debug (ri->length (0));
debug (string (sm.first, sm.second));
throw runtime_error ("not in dictionary");
}
if (ri->position (0) + ri->length (0) == text.length ()) // successfully delimited all the words in the text
break;
res.push_back (sm.str ());
}
if (ri != ri_end) throw runtime_error ("not in dictionary");
}
I expect this code to based on the elements of dict
, delimit the words in text
.
But why does it always skip the first word "i"
and goes to the second word "love"
, outputting 1 as the first value of ri->position (0)
?
My output:
pattern: i|love|china|lovechina|ilove
ri->position (0): 1
ri->length (0): 4
string (sm.first, sm.second): love
terminate called after throwing an instance of 'std::runtime_error'
what(): not in dictionary
Aborted (core dumped)
Probably just a stupid error.
New contributor
dizzy x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.