당신이 갖고있는 배열 선언은 의도 한 것이 아닙니다. int에 대한 포인터에 대한 포인터의 두 요소 배열이 있습니다. This page은 이러한 선언을 읽는 좋은 지침입니다.
는 개인적으로, 나는 바닥부터 다음과 같은 형식 정의를 사용하여 복잡한 형태를 구축하는 것을 선호 : 간단하게 할 수있는 방법은 아마도 있습니다
typedef int[2] element_type; // this is the 2-element array of ints
typedef element_type* inner_type; // this is the array of unknown size
typedef inner_type[5] outer_type; // this is the actual type we want to use
outer_type output_vertex; // we now have an array of 5 inner_type variables on the stack
// The output_vertex is *uninitialized* so we have to initialize each of its elements
for (int i=0; i < 5; ++i) {
output_vertex[i] = new inner_type[SOME_SIZE];
}
// do stuff with output_vertex now that it's initialized
// then, to prevent memory leaks, delete the memory you allocated
for (int i=0; i < 5; ++i) {
delete[] output_vertex[i];
}
,하지만 그건 시작해야합니다. 당신이 inner_type
가 추가가되고 싶다면
, 나는 강력하게 원시 배열 대신 std::vector
를 사용하는 것이 좋습니다 것이다. 원시 배열을 사용하여 수행해야 할 많은 부기가 있으므로, 나는 그 예제를 제시하지 않을 것이다; 그러나, 여기에 더 또는 덜 당신이 std::vector
으로 어떻게 할 것인지 :
typedef std::pair<int,int> element_type; // this is the 2-element array of ints as a pair
typedef std::vector<element_type> inner_type; // dynamic vector this time
inner_type output_vertex[5]; // we now have an array of 5 inner_type variables on the stack
// do stuff with output_vertex
std::vector
그냥 빨리 동적으로 할당 된 배열로,하지만 당신은 자신을 부기의 작업을 수행 할 필요가 없습니다. 또한 많은 힙 할당 객체를 관리 할 필요가 없다는 이점이 있습니다.
원시 배열은 컨테이너 (예 : std::vector
)와 호환되지 않으므로 여기서 std::pair
을 사용합니다.
C++ 11 (또는 부스트)를 사용할 수 있고 표준 컨테이너에 들어갈 수있는 두 개 이상의 고정 크기 배열이 필요한 경우 std::array
을 사용하십시오.
clipcounter가 0 인 경우에도 sorry.it는 작동하지 않습니다. (편집 됨) – code4fun
output_vertex에 2 명의 멤버를 할당하고 있지만 그 중 4 개를 반복하고 있습니다. 왜? – imreal
나는 그 부분이 잘 작동한다고 생각한다. 마지막 줄을 어 기고있다. – code4fun