Reputation: 51
Getting the error
no matching function for call to 'begin(int [n])'
I am using vectors and array and set but can't find out the reason for the error.
P.S. - I googled it but also couldn't find out anything relevant.
I tried debugging it but couldn't do it.
Here's my code!
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, flag = 0;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int index1 = distance(begin(a), find(begin(a), end(a), 2));
std::set<int> sa(a, a + n);
std::vector<int> vec(sa.size());
std::copy(sa.begin(), sa.end(), vec.begin());
int arr[vec.size()];
copy(vec.begin(), vec.end(), arr);
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < n; j++) {
if (arr[i] == a[j]) {
int index1 = distance(begin(a), find(begin(a), end(a), i));
int index2 = distance(begin(a), find(begin(a), end(a), j));
if (index1 < n && index2 < n) {
flag = 1;
break;
}
}
}
}
if (flag) { cout << "Truly Happy\n"; }
else if (!flag) {
cout << "Poor Chef\n";
}
}
return 0;
}
Upvotes: 2
Views: 1060
Reputation: 32847
Your problem is variable sized arrays, which are not part of C++ standards, and you are using it in your code.
Use std::vector<> instead of them. That means, change this
int a[n]; // to ----------------------------> std::vector<int> a(n);
and this line
std::set<int> sa(a, a + n); // to ----------> std::set<int> sa(a.begin(), a.end());
also
int arr[vec.size()]; // to -----------------> std::vector<int> arr(vec.size());
copy(vec.begin(), vec.end(), arr); // to ---> copy(vec.begin(), vec.end(), arr.begin());
then your code will work.
However,
Here
for (int i = 0; i < n; i++) { scanf("%d", &a[i]); }
it looks like you are trying to have an array filling elements from
0, 1, 2,... , n-1
. This could be easily done by
std::iota. That means, the following is equivalent to the for loop
, which you wrote.
std::iota(a.begin(), a.end(), 0);
Secondly, you are doing so many copying, which does not look like a
good algorithm. Especially, coping with the set sa
and again coping it back to another vector(vec
). This is definitely, not you
want I guess.
You do not need to use std::find
on vector/ array a
. Since it is
in sorted order the relation between array index and array
element is of difference 1
, make use of this information to find
the index.
PS: Do not use #include<bits/stdc++.h>
, see this post for more info and
using namespace std; is not a good coding practices.
Upvotes: 1