왜 std :: begin() 및 std :: end()은 배열과 관련하여 작동하지만 포인터 [거의 배열입니다] 및 배열 [원래 배열의 별칭]의 참조는 작동하지 않습니다.std :: begin 및 std :: end가 포인터와 참조로 작동하지 않는 이유는 무엇입니까?
내 머리를 15 분 동안 긁어 낸 후에는 Google에서 아무 것도 얻을 수 없습니다.
아래의 첫 번째 사례는 두 번째 및 세 번째가 아닌 다음과 같은 이유 때문일 수 있습니다.
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
int first[] = { 5, 10, 15 }; // Fist Case
if (std::find(std::begin(first), std::end(first), 5) != std::end(first)) {
std::cout << "found a 5 in array a!\n";
}
int *second = new int[3]; // Second Case
second[0] = 5;
second[1] = 10;
second[2] = 15;
if (std::find(std::begin(second), std::end(second), 5) != std::end(second)) {
std::cout << "found a 5 in array a!\n";
}
int *const&refOfFirst = first; // Third Case
if (std::find(std::begin(refOfFirst), std::end(refOfFirst), 5) != std::end(refOfFirst)) {
std::cout << "found a 5 in array a!\n";
}
}
오류 : 배열의 시작에 불과 포인터를 감안할 때
error: no matching function for call to ‘begin(int&)’
if (std::find(std::begin(*second), std::end(*second), 5) != std::end(*second)) {
^
세 번째 경우는 배열이 아니라 포인터를 참조합니다. –
언제부터 포인터가 "거의 배열"입니까? –