#include <iostream> #include <string> using namespace std; class Nnumber : std::unary_function<char, bool>{ static int k; public: bool operator ()(char ch) { if (isdigit(ch)) return false; if ( ch == ',' || ch == '.') { ++k; if(k < 2) return false; else return true; } return true; } }; int Nnumber::k = 0; void remake(string& s) { s.erase(remove_if(s.begin(), s.end(), Nnumber()), s.end()); } int main(int argc, char *argv[]) { QCoreApplication qca(argc, argv); string s("123, rt45,6j"), s1("567,.. 87"); remake(s); remake(s1); cout << s <<'\n' // "123,457" << s1; // "56787" ??? нет символа ',' return qca.exec(); } 

I need the string to represent a rational number of arbitrary length, and for this I am trying to exclude any unnecessary character from there. I expected s1 == "567.87", however it is not. I did not understand what the error was. Help please fix

  • 2
    As a side note: std::unary_function is deprecated and it is no longer necessary to use it. - AnT
  • @AnT, thanks, I will know - AR Hovsepyan
  • ... if I'm not mistaken, in C ++ 17 std::unary_function and std::binary_function are no longer at all. - AnT
  • @AnT, maybe because there is a lot of new things being said (I haven’t watched it yet - I’m just newbies) - AR Hovsepyan

3 answers 3

It is logical, because k - you have a static variable, and between different calls its value is saved. Do not forget to reset it and everything will be ok.

PS

view code

 if(k < 2) return false; else return true; 

better to replace

 return (k>=2); 
  • I agree, but this is just an improvement in the record, and not a correction of a logical error, I'm sorry, I hurried! Nalo did not read ... - AR Hovsepyan
  • and in what place to nullify? - AR Hovsepyan
  • added condition in question - AR Hovsepyan
  • it is probably necessary to reset the variable in the remake call (only k is private, therefore either it is necessary to make it public, or to make a function for this). In this case, it returns the result you expect. But it's better, of course, to make the variable non-static and reset to zero in the constructor. But there the result is different, but I don’t want to understand why. The improvement that I proposed in the code is precisely an improvement and simplification (and I did not write that this is a bug fix). - KoVadim
  • I understood, thanks - I missed that after the first call the static member has already been changed ... - AR Hovsepyan

?

 #include <iostream> #include <string> using namespace std; class Nnumber : std::unary_function<char, bool>{ public: Nnumber(): k(0) {} bool operator ()(char ch) { if (isdigit(ch)) return false; if ( ch == ',' || ch == '.') { ++k; if(k < 2) return false; else return true; } return true; } }; void remake(string& s) { s.erase(remove_if(s.begin(), s.end(), Nnumber()), s.end()); } int main(int argc, char *argv[]) { QCoreApplication qca(argc, argv); string s("123, rt45,6j"), s1("567,.. 87"); remake(s); remake(s1); cout << s <<'\n' // "123,457" << s1; // "56787" ??? нет символа ',' return qca.exec(); } 
  • With your code, all commas and periods will be in a line, but you need only the first comma or period to remain. Sorry, now add to the question - AR Hovsepyan
  struct Nnumber : std::unary_function<char, bool>{ static int k; bool operator ()(char ch) { if (isdigit(ch)) return false; if ( ch == ',' || ch == '.') { ++k; return k >= 2; } return true; } }; int Nnumber::k = 0; void remake(string& s) { s.erase(remove_if(s.begin(), s.end(), Nnumber()), s.end()); Nnumber::k = 0; }