如何迭代作为指针传递的对数组参数

how do i iterate the pair array arguments passed as a pointer?

本文关键字:参数 数组 指针 何迭代 迭代      更新时间:2023-10-16

如何迭代作为指针传递的对数组参数?

我曾经试着用作为参考对&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 yes(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);

我建议放弃VLA(它无论如何都不是C++的一部分(,改用std::vector。包括<vector>并将声明更改为:

std::vector<std::pair<lli, lli>> a(n);

函数的签名为:

void yeah(std::vector<std::pair<lli, lli>> &arr)