vaibhavg_21's blog

By vaibhavg_21, history, 3 years ago, In English

For reading a vector or any STL in C++ I was initialliy using a marco

#define read(x) for(auto &inps: x) cin>>inps
int n = 5;
vector<int> a(n),b(n),c(n);
read(a);
read(b);
read(c);

But for reading multiple vectors I want to implment a kind of function or macro that does something like

int n = 5;
vector<int> a(n),b(n),c(n);
read(a,b,c);

Can someone help me that how to implement this? I have tried to implement this using templates and macros but i am facing error. Here I have attached the code that I had implemented.

#define read(args...) read1(args)

template<typename T, typename... Args>
void read1(T &a, Args... args) {
    for(auto &i:a) cin >> i;
    read1(args...);
}
  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

add base case and pass args in function by reference

code