0
#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 .

5
  • 2
    pre and a are 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). Commented May 2, 2023 at 21:00
  • 1
    Time to learn about std::vector. As a bonus this gets rid of junk like memset(). Commented May 2, 2023 at 21:03
  • PSA: In C++ prefer nullptr to C's typeless NULL. Commented May 2, 2023 at 21:04
  • 1
    Unrelated: Seeing #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. Commented May 2, 2023 at 21:14
  • 1
    Somewhere out there there must be a using namespace std in order for the compiler to get as far as it did. Prefer not to use it as well. When you combine using namespace std; with #include <bits/stdc++.h> you're really ramping up the odds of a negative user interaction with the compiler. Commented May 2, 2023 at 21:17

1 Answer 1

0

The problem with your code is that you are using variable length arrays

  int n, q;
  cin>>n>>q;
  int pre[n], a[n];

Variable length arrays are not a standard C++ feature.

The variable n is 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.

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

template <typename T>
std::istream &operator >>( std::istream &in, std::vector<T> &v )
{
    std::for_each( std::begin( v ), std::end( v ), 
        [&in]( auto &elem ) 
        {
            in >> elem;
        } );

    return in;
}

int main()
{
    size_t n = 5;
    std::vector<int> v( n );

    std::cout << "Enter " << n << " values: ";
    std::cin >> v;

    for (const auto &elem : v)
    {
        std::cout << elem << ' ';
    }
    std::cout << '\n';
}

The program output is

Enter 5 values: 1 2 3 4 5
1 2 3 4 5

Also pay attention to that the casting the integer constant 0 of the type int to the type int in the return statement

return (int)0;

does not make sense. Just write

return 0;

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

cin >> a;

like

std::cin >> a;

And it is unclear what thsi directive

#undef int

is doing in your program,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.