Skip to content

Instantly share code, notes, and snippets.

@prostoiChelovek
Created June 12, 2019 10:37
Show Gist options
  • Save prostoiChelovek/643501e25927616856d4de722011144f to your computer and use it in GitHub Desktop.
Save prostoiChelovek/643501e25927616856d4de722011144f to your computer and use it in GitHub Desktop.
vector<int> string2int(const string &str) {
vector<int> res;
string temp = str;
map<string, int> oneDgtNums = {{"ноль", 0},
{"один", 1},
{"два", 2},
{"три", 3},
{"четыре", 4},
{"пять", 5},
{"шесть", 6},
{"семь", 7},
{"восемь", 8},
{"девять", 9}};
map<string, int> twoDgtNums = {{"десять", 10},
{"одинадцать", 11},
{"двенадцать", 12},
{"тринадцать", 13},
{"четырнадцать", 14},
{"пятнадцать", 15},
{"шестнадцать", 16},
{"семнадцать", 17},
{"восемнадцать", 18},
{"девятнадцать", 19},
{"двадцать", 20},
{"тридцать", 30},
{"сорок", 40},
{"пятьдесят", 50},
{"шестьдесят", 60},
{"семьдесят", 70},
{"восемьдесят", 80},
{"девяносто", 90}};
vector<unsigned long> twoDgtNumsPos;
for (const auto &s : twoDgtNums) {
auto pos = temp.find(s.first);
if (pos != string::npos) {
temp.replace(pos, s.first.size(), to_string(s.second));
twoDgtNumsPos.emplace_back(pos);
}
}
vector<unsigned long> oneDgtNumsPos;
for (const auto &s : oneDgtNums) {
auto pos = temp.find(s.first);
if (pos != string::npos) {
temp.replace(pos, s.first.size(), to_string(s.second));
oneDgtNumsPos.emplace_back(pos);
}
}
for(const auto &pos : twoDgtNumsPos) {
for(const auto &oPos : oneDgtNumsPos) {
if(pos + 3 == oPos) {
temp.replace(pos + 1, pos + 2, "");
}
}
}
for(const string &s : split(temp, " ")) {
try {
res.emplace_back(stoi(s));
} catch(...) {
continue;
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment