#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;
}
上面在使用运算符重载获取数组输入的解决函数中编写的 cin 代码不起作用,为什么会这样?我正在尝试对输入流运算符 cin 使用运算符重载,并尝试使用它来输入一个数组,然后对其进行处理。所以 cin cin 总体上减少了时间……很多。 我参考了Link .
您的代码的问题是您使用的是可变长度数组
int n, q;
cin>>n>>q;
int pre[n], a[n];
变长数组不是标准的 C++ 特性。
变量
n
不是编译时常量。它不能用作非类型模板参数。
而是使用标准容器
std::vector
.
还要注意在return语句中将
0
类型的整型常量int
强制转换为int
类型
return (int)0;
没有意义。随便写
return 0;
或者您甚至可以删除退货声明。
而且这个头文件
<bits/stdc++.h>
不是标准的C++头文件,在你的程序中是多余的。删除它,而不是包含标题<iterator>
.
因为没有 using 指令,也没有 using 声明,所以在此语句中使用限定符名称
cin >> a;
喜欢
std::cin >> a;
目前还不清楚这个指令是什么
#undef int
在你的程序中做,