c++compiler-errorsoperator-overloadingvariable-length-array

Taking Input an array in C++ using cin directly by operator overloading


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


Solution

  • 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,