얼마나 많은 가치를 찾을 수 있는 방법이 있는 어레이에는? # 39, ve 도달했음 여부를 감지해 I& 어레이에서는 작동합니까 말 수도 있다는 것이다.
그런 경우 c 스타일 어레이입니다 할 수 있는 것은 다음과 같은 항목을
int a[7];
std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;
int *p = new int[7];
std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
또는:
void func(int *p)
{
std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
}
int a[7];
func(a);
C++에서, 스케쳐내, 그러면 이 같은 행동에 컨테이너입니다 클래스를 사용해야 합니다. 아마도 ',' (http://en.cppreference.com/w/cpp/container/vector"), a href = " < > 표준용량 벡터입니다 < /a>.;;;
template<class T, size_t N>
constexpr size_t size(T (&)[N]) { return N; }
이를 위해 컴파일하십시오 비사양 어레이입니다 못한 좋은 속성 유형 (visual studio 가 않는 ['_countof'] (http://msdn.microsoft.com/en-us/library/ms175773.aspx) 이). 이 ['콘스테스파르'] (http://en.cppreference.com/w/cpp/language/constexpr) 는 아무런 단점 때문에 이 컴파일 타임 표현식에서는 doesn& # 39 위에, t 매크로 (적어도 내가 아는 없음).
C++11 길이를 알 수 있는 '표준용량 어레이입니다 사용을 고려해보십시오 수도 있습니다' 를 통해 오버헤드에 없이 넷윈을 캜 어레이입니다.
>, 얼마나 많은 값을 찾을 수 있는 방법이 있는 어레이에는?
구로췌!
스이제로프 (어레이입니다 [0]) '' 시도하시겠습니까 스이제로프 (array) /
나는 이 길을 너회의 어레이입니다 배열입니다 않으면 표시되지 않습니다 문자 (이리에 string).
P. S. 항상 flfile. c++컴파일러는 표준용량 벡터입니다 '에서'. 붙박이 몇 가지 기능 및 확장된 기능을.
하지만 이 질문에 대한 답을 업데이트하던 규모의, s # 39 는 오래된 it& C++17. 이제 이 표준 템플릿 라이브러리 함수 ['표준용량 크기 ()'] (http://en.cppreference.com/w/cpp/iterator/size) 를 모두 요소의 수를 되돌려줍니다 표준용량 컨테이너입니다 또는 c 스타일 어레이입니다. 예를 들면 다음과 같습니다.
#include <iterator>
uint32_t data[] = {10, 20, 30, 40};
auto dataSize = std::size(data);
// dataSize == 4
C++11 이후, 몇 가지 새로운 템플릿일 고통을 줄일 수 있는 길이 다룰 때 어레이입니다 소개한다. ',', Type_traits> < 헤더입니다 모두야 정의되어 있다.
만약 'T' 는 멤버 수와 같은 값을 상수입니다 배열입니다 유형, 크기에 어레이입니다. 다른 유형, 값이 0 인.
만약 'T' 은 항상 같은 값을 배열입니다 유형에 따라 여러 요소를 멤버 '의 경우 n'th 치수처리 어레이이며' N '가 [0, std::rank< 가치 t>,', '). 만약 'T' 는 다른 유형이거나 어레이입니다 바인딩되지 알 수 없는 'N' 는 0, 1 치수이고 따라 값이 0 인.
만약 'T', 'X' 는 일부 유형을 배열입니다 유형 'X' 는 'T' 같은 멤버 메리 페데프, 그렇지 않으면 유형. 만일 'T' 는 다차원 배열, 오직 제 1 치수처리 제거됩니다.
만약 'T' 는 일부 유형 'X' 은 다차원 배열 유형에 'X' 는 'T' 같은 멤버 메리 페데프, 그렇지 않으면 유형.
',' 의 afaq 디스테이징하는 치수처리 멀티디멘시알 어레이입니다 데클티페 사용될 수 있는 '어느 정도 표준용량 결합한'. 예를 들면 다음과 같습니다.
#include <iostream>
#include <type_traits> // std::remove_extent std::remove_all_extents std::rank std::extent
template<class T, size_t N>
constexpr size_t length(T(&)[N]) { return N; }
template<class T, size_t N>
constexpr size_t length2(T(&arr)[N]) { return sizeof(arr) / sizeof(*arr); }
int main()
{
int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
// New way
constexpr auto l1 = std::extent<decltype(a)>::value; // 5
constexpr auto l2 = std::extent<decltype(a), 1>::value; // 4
constexpr auto l3 = std::extent<decltype(a), 2>::value; // 3
constexpr auto l4 = std::extent<decltype(a), 3>::value; // 0
// Mixed way
constexpr auto la = length(a);
//constexpr auto lpa = length(*a); // compile error
//auto lpa = length(*a); // get at runtime
std::remove_extent<decltype(a)>::type pa; // get at compile time
//std::remove_reference<decltype(*a)>::type pa; // same as above
constexpr auto lpa = length(pa);
std::cout << la << ' ' << lpa << '\n';
// Old way
constexpr auto la2 = sizeof(a) / sizeof(*a);
constexpr auto lpa2 = sizeof(*a) / sizeof(**a);
std::cout << la2 << ' ' << lpa2 << '\n';
return 0;
}
총 한 요소로 멀티디멘티오날 어레이입니다 베티 얻을 수 있습니다.
constexpr auto l = sizeof(a) / sizeof(std::remove_all_extents<decltype(a)>::type);
함수 또는 넣어 템플리트를:
#include <iostream>
#include <type_traits>
template<class T>
constexpr size_t len(T &a)
{
return sizeof(a) / sizeof(typename std::remove_all_extents<T>::type);
}
int main()
{
int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
constexpr auto ttt = len(a);
int i;
std::cout << ttt << ' ' << len(i) << '\n';
return 0;
}
링크 따라 이를 이용하는 것이 더 많은 사례를 찾을 수 있다.
const std::string s[3] = { "1"s, "2"s, "3"s };
constexpr auto n = std::extent< decltype(s) >::value; // From <type_traits>
constexpr auto n2 = std::extent_v< decltype(s) >; // C++17 shorthand
const auto a = std::array{ "1"s, "2"s, "3"s }; // C++17 class template arg deduction -- http://en.cppreference.com/w/cpp/language/class_template_argument_deduction
constexpr auto size = std::tuple_size_v< decltype(a) >;
std::cout << n << " " << n2 << " " << size << "\n"; // Prints 3 3 3
내장 함수를 사용하는 대신 어레이당 lc-fc:
int x[2] = {0,1,2};
이 어레이에는 콩지름에 클래스 및 어레이입니다 템플리트를. 종료기:
#include <array>
array<type_of_the_array, number_of_elements_in_the_array> Name_of_Array = {};
이제 네가 해야 할 일은 이 어레이에는 확인표시를 com/go/downloads_kr 디스테이징하는 배열의 크기를 사용하면 함수은 구분된다.
Name_of_Array.size();
그리고 반품해야 하는 길이 요소의 어레이입니다.
C++에서, 표준용량 어레이입니다 클래스를 사용하여 쉽게 찾을 수 있을 뿐만 아니라, 하나의 크기가 너희가운데 어레이에서는 어레이에서는 마지막 요소.
#include<iostream>
#include<array>
int main()
{
std::array<int,3> arr;
//To find the size of the array
std::cout<<arr.size()<<std::endl;
//Accessing the last element
auto it=arr.end();
std::cout<<arr.back()<<"\t"<<arr[arr.size()-1]<<"\t"<<*(--it);
return 0;
}
[1]: kw (http://www.cplusplus.com/reference/array/array/) = 어레이입니다?
[구글 프로토부] '의 한 라이시즈 슬라이드에서는 구축' (http://osxr.org/android/source/external/protobuf/src/google/protobuf/stubs/common.h).
#define GOOGLE_ARRAYSIZE(a) \
((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
// test codes...
char* ptr[] = { "you", "are", "here" };
int testarr[] = {1, 2, 3, 4};
cout << GOOGLE_ARRAYSIZE(testarr) << endl;
cout << GOOGLE_ARRAYSIZE(ptr) << endl;
>. 스이제로프 (도착) 는 라이시즈 확인하여 (도착) 에 있는 # 바이트 array) 와 스이제로프 (* (도착)) (이 한 어레이입니다 # 바이트 요소). 이 경우, 아마도 도착 전 rmw 의해 수 있다.
요소를 어레이입니다. 그렇지 않으면 어레이에서는 도착, 가능성이 높아 수 없습니다. 하나님은 컴파일러 오류가 발생하지 않도록 코드로부터 컴파일하기를.
>. 부울 구현 정의된 크기 때문에, 이 나라를 위해 필요하다. ! (& 스이제로프 (a). 스이제로프 (* (a))) 를 보장하기 위해 size_t 최종 size_t 결과: 유형이.
>. 이 때문에 특정 사용할 수 있는 자는 매크로 완벽할 순 없다 포인터는 포인터 크기는 수 있는, 즉 푸앵티 의해 크기. 이후 32 비트 컴파일러, 우리의 모든 코드를 통해 갈 수 있다. 즉, 모든 바이트입니다 포인터가 가리키는 그 곳에서 4 유형 크기는 3 또는 4 개 이상의 적립율은 (선) 거부됩니다.
C++/cx (C++ 우프 사용하여 애플리케이션을 작성할 때 예를 들어 비주얼 스튜디오에서) 의 수를 사용하기만 하면 어레이에서는 값을 찾을 수 있습니다 '크기 ()' 기능을.
string myArray[] = { "Example1", "Example2", "Example3", "Example4" };
int size_of_array=size(myArray);
If you '의' '코트 size_of_array 출력됩니다.
>>> 4
You have a bunch of 얻기 위해 사용될 수 있는 옵션이 캜 배열 크기.
'1) 스이제로프 (array> <;) / 스이제로프 (type> <;). '
std::cout << "Size:" << sizeof(myArray) / sizeof(int) << std::endl;
'2) 스이제로프 (array> <;) / 스이제로프 (array> *<;). '
std::cout << "Size:" << sizeof(myArray) / sizeof(*myArray) << std::endl;
'3) 스이제로프 (array> <;) / 스이제로프 (<, array> [< element>;]). '
std::cout << "Size:" << sizeof(myArray) / sizeof(myArray[0]) << std::endl;
단순히 이 스니핏을 사용할 수 있습니다.
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
array<int,3> values;
cout << "No. elements in valuea array: " << values.size() << " elements." << endl;
cout << "sizeof(myints): " << sizeof(values) << endl;
}
그리고 다음은 참조: http://www.cplusplus.com/reference/array/array/size/
좋은 솔루션이 사용하는 원본:
template <typename T,unsigned S>
inline unsigned arraysize(const T (&v)[S]) { return S; }
그럼 간단히 얻을 수 있는 '콜' 라이시즈 (_Array), 디스테이징하는 어레이입니다.
[출처] [1]
[1]: http://www.cplusplus.com/forum/general/33669/ # msg181103