Reputation: 1
So the problem states that I have to input an array of unsorted elements containing duplicates and I decided to use sort. I am new to this and I don't understand the error
I'm trying to run it on GFG's IDE.
#include <iostream>
#include <bits/stdc++.h>
int main() {
//code
int n,size,a;
cin>>n;
while(n--)
{
cin>>size;
int a[size];
set<int> s;
for(int i=0;i<size;i++)
{
cin>>a;
s.insert(a);
}
for(int i=s.begin();i!=s.end();i++)
cout<<i<<" ";
return 0;
}
Error message:
no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'int [size]')
cin>>a;
Upvotes: 0
Views: 105
Reputation:
The source of the error you show is this line right here:
int main() {
//...
int a[size];
// ...
}
a
is an array now, which has no operator>>()
overloaded for it for std::istream
.
The solution is simple, just change this line:
cin>>a;
to this:
cin>>a[i];
Now, there should be no compiling problems, although a ton of bad practices still, which several answers and other comments and an answer has attempted to correct. I'll leave that to them.
The only thing I will warn you about is
int a;
some block {
int a[size];
}
// try to use a:
a[i]; // bad
There are 2 variables named a
in your program. Once the second a goes out of scope, it will destroy the array version of a
, and use the regular int a
instead. Just be wary of this.
P.S. Okay, so I will mention another one that seems unmentioned by others.
The following is ill formed, since it tries to use a variable-length array:
cin>>size;
int a[size];
Some compilers will support it, but technically, that is an ill-formed program. As a general rule, don't do it.
Upvotes: 1