#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
template<typename T, size_t Size>
std::istream& operator>>(std::istream& in, T (&arr)[Size])
{
std::for_each(std::begin(arr), std::end(arr), [&in](auto& elem) {
in >> elem;
});
return in;
}
void solve()
{
int n, q;
cin>>n>>q;
int pre[n], a[n];
memset(pre, 0, sizeof(pre));
cin >> a; // this statement is not working giving compilation error as:-
"no operator ">>" matches these operands C/C++(349)
a.cpp(167, 7): operand types are: std::istream >> long long [n]"
}
#undef int
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--)
solve();
return (int)0;
}
The above written cin code in the solve function for taking an array input using operator overloading is not working, why so? I am trying to use the operator overloading for input stream operator cin and trying to use it to input an array and then process it over. So overall reducing time by cin cin ... a lot. I have taken reference from Link .
preandaare not arrays. They are Variable Length Arrays, entirely different thing that's not even part of C++ (some compilers unfortunately accept it as an extension).std::vector. As a bonus this gets rid of junk likememset().nullptrto C's typelessNULL.#include <iostream>and#include <algorithm>alongside#include <bits/stdc++.h>strongly suggest that you know not what#include <bits/stdc++.h>is for. Here's some reading on that. It's often an unforced error, so prefer to include the headers you need, no more and no less.using namespace stdin order for the compiler to get as far as it did. Prefer not to use it as well. When you combineusing namespace std;with#include <bits/stdc++.h>you're really ramping up the odds of a negative user interaction with the compiler.