How to implement own function to read int?

Правка en2, от Qualified, 2020-10-06 00:18:45

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
Теги help, fast input, overloading, oistream

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Qualified 2020-10-06 00:18:45 758
en1 Английский Qualified 2020-10-06 00:07:46 327 Initial revision (published)