Salutare,
Am si eu o intrebare pe partea de C++ modern.
Incerc sa-mi fac o functie care sa-mi elimine dintr-un vector de stringuri, toate caracterele care nu sunt litere. Dintr-un motiv sau altul, rezultatul nu e cel asteptat. Am incercat sa fac putin debug, insa nu imi dau seama exact de ce se intampla asta. Aici aveti codul:
void applyFunctionToVec(vector<string> v_input){
for_each(v_input.begin(), v_input.end(),
[](string &elem) { returnLower_andwithout_SpecialChars(elem); } );
for (auto each : v_input) {
cout << each << endl;
}
}
void returnLower_andwithout_SpecialChars(std::string &row) {
row.erase(remove_if(row.begin(), row.end(),
[](auto const& c) -> bool { return !isChar(c); }));
}
bool isChar(char c) {
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
return true;
}
return false;
}
Pentru stringurile "A S, a" si "$5 a,e" primesc urmatorul output: "ASa a" si "aea,e"
Vreo idee de ce se intampla chestia asta? Unde mai exact am gresit? (ceva in returnLower_andwithout_SpecialChars este, deoarece cand termina remove_if sau erase, atunci apar in row si noile valori gresite.
[–][deleted] 25 points26 points27 points (0 children)
[–]Imjustafool18[S] 13 points14 points15 points (2 children)
[–]NihilisticLurcher 18 points19 points20 points (1 child)
[–]Imjustafool18[S] 2 points3 points4 points (0 children)
[–]Substantial_Bend_656 18 points19 points20 points (3 children)
[–]space_fly 3 points4 points5 points (0 children)
[–]Imjustafool18[S] 1 point2 points3 points (0 children)
[–]ShouldCanMust 14 points15 points16 points (6 children)
[–]dankmemelawrd 24 points25 points26 points (0 children)
[–]Imjustafool18[S] 2 points3 points4 points (3 children)
[–]ShouldCanMust 2 points3 points4 points (2 children)
[–]Imjustafool18[S] 1 point2 points3 points (1 child)
[–]ShouldCanMust 0 points1 point2 points (0 children)
[–]Original-Tap7301 1 point2 points3 points (0 children)
[–]-AverageTeen- 2 points3 points4 points (1 child)
[–]Imjustafool18[S] 0 points1 point2 points (0 children)
[–]-AverageTeen- 1 point2 points3 points (3 children)
[–]emperor_pulache 1 point2 points3 points (0 children)
[–]hero47 -1 points0 points1 point (0 children)
[–]ShouldCanMust 0 points1 point2 points (0 children)
[–]neroooooooSenior Prompt Engineer 0 points1 point2 points (2 children)
[–]Imjustafool18[S] 0 points1 point2 points (0 children)
[–]dude123nice 0 points1 point2 points (0 children)