c++ - 如何迭代作为指针传递的对数组参数?
问题描述
如何迭代作为指针传递的对数组参数?
我曾尝试用作参考对 &arr 。但它也不起作用。我可以pair<lli,lli> a[n];
作为参考吗?
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
typedef long long int lli;
typedef unsigned long long int ulli;
void yeah( pair<lli,lli> *arr ){
// cout << arr[0].ff; 100
//this doesnt work :(
for(auto e : arr){
cout << e.ff << " " << e.ss << endl;
}
}
int main() {
int n = 10;
pair<lli,lli> a[n];
a[0].ff = 100;
a[1].ss = 150;
yeah(a);
}
这是我得到的错误
prog.cpp:在函数'void是(std :: pair )'中:prog.cpp:13:18:错误:没有匹配函数调用'begin(std :: pair &)'for(auto e:arr) { ^ ^
解决方案
具有固定大小数组的可能解决方案:
template<std::size_t size>
void foo(std::pair<int, int> (&arr)[size]) {
for (auto e : arr) {
...
}
}
constexpr std::size_t n = 10; // should be known at compile time
std::pair<int, int> a[n];
foo(a);
推荐阅读
- c - 不知道为什么文件打不开
- keycloak - Keycloak 12 - 限制用户访问账户控制台
- typescript - 如何在 VSCode 扩展开发中向 QuickPick 添加新元素?
- python - 从 github 代码组织中克隆所有代码仓库的 python 脚本
- javascript - 每次使用同一个 JavaScript 函数时,如何获得一个新值?
- python - 如何使用 lambda 和 python 在 s3 上压缩文件
- python - 检查 get_source() 在作为参数传递给函数并且换行时不返回完整的 lambda 代码?
- amazon-web-services - 我正在使用带有颤振框架的aws。我试图删除一个auth cognito pool并尝试放大推送然后得到这个错误请帮我解决
- javascript - 页面加载后的 Cheerio 选择器
- angular - 步进器材料:在打字稿中使用 stepper.reset()