gpt4 book ai didi

C++ begin/end(arr) on a pointer - 没有匹配函数来调用 ‘begin(int**&)’

转载 作者:行者123 更新时间:2023-11-27 22:48:04 27 4
gpt4 key购买 nike

<分区>

我想获取数组的长度。我有这段代码:

#include <iterator>
#include <iostream>
#include <string>
using namespace std;

void printArrLen(int arr[]);

int testArr [3] = {1, 4, 5};

int main() {
printArrLen(testArr);
cout << "main/testArr; Memory address: " << testArr << ", value: " << *testArr << endl;
cout << end(testArr) << endl;
}

void printArrLen(int arr[]) {
cout << "printArrLen/arr; Memory address: " << arr << ", value: " << *arr << endl;
cout << "printArrLen/testArr; Memory address: " << testArr << ", value: " << *testArr << endl;
// This works:
cout << end(testArr) << endl;
// This doesn't work - no matching function for call to 'end(int*&)':
// cout << end(arr) << endl;
// Doesn't work:
// cout << "arrLen: " << end(arr) - begin(arr) << endl;
}

输出:

printArrLen/arr; Memory address: 0x601318, value: 1
printArrLen/testArr; Memory address: 0x601318, value: 1
0x601324
main/testArr; Memory address: 0x601318, value: 1
0x601324

取消注释 cout << end(arr) << endl;在 printArrLen 中给出 no matching function for call to 'end(int*&)'

我知道 begin/end(arr)如果 arr 将无法工作是一个指针。为什么他们在 printArrLen 和 main 中处理 testArr,如果 testArr 似乎也是一个指针?如何在 printArrLen 中证明 testArr不是指针并且 arr是同时它们似乎都包含一个内存地址?

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