#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 .
The problem with your code is that you are using variable length arrays
Variable length arrays are not a standard C++ feature.
The variable
nis not a compile time constant. It may not be used as a non-type template argument.Instead use standard container
std::vector.With vector you could use just range-based for loop to enter values. If nevertheless you want to implement your approach with the overloaded operator then it can look the following way as shown in the demonstration program below.
The program output is
Also pay attention to that the casting the integer constant
0of the typeintto the typeintin the return statementdoes not make sense. Just write
Or you may even remove the return statement.
And this header
<bits/stdc++.h>is not a standard C++ header and is redundant in your program. Remove it and instead of it include header<iterator>.And as there is no using directive and nor using declaration then use the qualifier name in this statement
like
And it is unclear what thsi directive
is doing in your program,