Блог пользователя ivan100sic

Автор ivan100sic, история, 2 года назад, По-английски

I'll just leave this here:

#include <bits/stdc++.h>
using namespace std;

struct _in {
    template<class T> operator T() {
        T x;
        cin >> x;
        return x;
    }
} in;

int main() {
    vector a(in, 0.0);
    for (auto& x : a) x = in;
    string s = in;
    cout << s << ' ' << a[2] << '\n';
}

Try with input

5
0.1 0.2 0.3 0.4 0.5
hello

Have a nice day

  • Проголосовать: нравится
  • +256
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

this is like, really cool

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

struct can be anonymous which makes it even cooler.

»
2 года назад, # |
  Проголосовать: нравится +55 Проголосовать: не нравится

Unfortunately, this may lead to non-obvious errors like as follows:

void f(int a, int b)
{
    cout << a << ' ' << b << '\n';
}

int main()
{
    f(in, in);
    return 0;
}

Input: 1 2
Output: 2 1

»
2 года назад, # |
  Проголосовать: нравится +21 Проголосовать: не нравится

Thanks for sharing this! Just a small improv: you can omit typename like that:

struct {
  template <class T> operator T() {
    T x;
    cin >> x;
    return x;
  }
} in;