I very much like $$$cin » $$$ but want to have this fast reading integers function. How to do it? For example, I want to input multiple integers in this $$$cin » a » b » c;$$$ but the reading of integers is using the comment above.
I tried this but it didn't work.
istream & operator >> (istream& os, int x) {
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus)
os >> -result;
else
os >> result;
return os;
}
But I got this error
error: ambiguous overload for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'int')
124 | os >> result;
| ~~ ^~ ~~~~~~
| | |
| | int
Auto comment: topic has been updated by Qualified (previous revision, new revision, compare).
You cannot overload a previously declared function with the same arguments. The only way to create your own class.
The implementation looks like this. Not pretty, but it works.