gpt4 book ai didi

c++ - 没有匹配函数调用 'begin(int [n])'

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:54:57 27 4
gpt4 key购买 nike

获取错误

no matching function for call to 'begin(int [n])'

我正在使用vectorsarrayset但是找不到错误的原因。 附言- 我用谷歌搜索但也找不到任何相关内容。我尝试调试它但无法做到。这是我的代码!

#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;
}

最佳答案

您的问题是可变大小数组are not part of C++ standards ,并且您正在代码中使用它。

使用std::vector<>而不是他们。也就是说,改变这个

int a[n]; // to ----------------------------> std::vector<int> a(n);

还有这一行

std::set<int> sa(a, a + n); // to ----------> std::set<int> sa(a.begin(), a.end());

还有

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());

那么您的代码就可以工作了。

但是,

  1. 这里

    for (int i = 0; i < n; i++) {  scanf("%d", &a[i]);     }

    看起来你正试图用一个数组填充来自 0, 1, 2,... , n-1 .这可以很容易地完成 std::iota .这意味着,以下等同于 for loop ,你写的。

    std::iota(a.begin(), a.end(), 0);
  2. 其次,你复制了这么多,看起来不像是一个好的算法。特别是,应对集合 sa并再次将其处理回另一个 vector (vec)。这绝对不是你我猜想。

  3. 您不需要使用 std::find在 vector/数组上 a .因为它是数组索引数组之间的排序顺序元素有区别1 , 利用此信息查找索引。


PS:不要使用#include<bits/stdc++.h> , 请参阅 this post for more infousing namespace std;不是好的编码习惯。

关于c++ - 没有匹配函数调用 'begin(int [n])',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138187/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com