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

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

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

我曾尝试将 &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: In function 'void yeah(std::pair)': prog.cpp:13:18: error: no matching function for call to 'begin(std::pair&)' for(auto e : arr){ ^ ^

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

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

函数签名为:

void yeah(std::vector<std::pair<lli, lli>> &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);