find - Search a string for a wildcard year C++ -
i'm looping through text file, reading each paragraph string. want process paragraphs contain year, if no year found want continue looping through file. when year found, want know index year found.
i'm trying avoid boost or regex code simplicity. assume years of interest in 1900s , 2000s, simplicity. tried following code, wildcard characters not working reason. is because wildcard characters not work numbers?
string sparagraph = "aramal et al. (2011), title"; int iindex; if (sparagraph.find("19??")!=string::npos) iindex = sparagraph.find("19??"); else if (sparagraph.find("20??")!=string::npos) iindex = sparagraph.find("20??"); else continue;
without using regex or boost code might making code more readable, not more simple.
a "simple" one-pass pseudo algorithm:
map<int, std::vector<int>> years; string par = " ... " //inefficient didn't want have add more complicated code //in while loop. want solution clear int par_index = par.find_first_of("19"); if(par_index == string::npos) par_index = par.find_first_of("20"); if(par_index == string::npos) //skip //no years in paragraph while(par_index < par.size()) { string year(par, par_index, 4); int year = atoi(year.c_str()); //or use c++11 stoi if(2100 < year && year >= 1900) years.at(year).push_back(par_index); par_index += 4; }
this create map key year , value vector of ints represent index year landed on.
Comments
Post a Comment