나는 이것에 대한 내 자신의 함수를 작성하려고하지만 잘못된 결과이 같은C++에서 배열의 길이를 얻는 방법은 무엇입니까?
#include <iostream>
using namespace std;
template<typename T>
int array_length(T v[]) {
return (sizeof v)/sizeof(T);
}
int main() {
int v[] = {1, 2, 3, 4};
cout << array_length(v) << endl;
return 0;
}
https://gist.github.com/3959946 마지막 기능을 참조하십시오. –
http://stackoverflow.com/questions/3368883/how-does-this-size-of-array-template-function-work – relaxxx
@ R.MartinhoFernandes :이 함수는 컴파일러가 크기를 알고 있어도 런타임에 크기를 제공합니다. 컴파일시 그 자체. 따라서 좋은 구현이라고는 말할 수 없습니다. – Nawaz