0
다음은 아래 코드입니다.벡터 값에 대해
class dimension;
typedef shared_ptr<vector<dimension*> > PVecDim;
typedef vector<dimension*> VecDim;
typedef vector<dimension*>::iterator VecDimIter;
struct dimension {
int height, width, length;
dimension(int h, int w, int l) : height(h), width(w), length(l) {
}
};
PVecDim getModList(PVecDim inList) {
PVecDim modList(new VecDim());
VecDimIter it;
for(it = inList->begin(); it!=inList->end(); ++it) {
dimension rot1((*it)->length, (*it)->width, (*it)->height);
dimension rot2((*it)->width, (*it)->height, (*it)->length);
cout<<"rot1 "<<rot1.height<<" "<<rot1.length<<" "<<rot1.width<<endl;
cout<<"rot2 "<<rot2.height<<" "<<rot2.length<<" "<<rot2.width<<endl;
modList->push_back(*it);
modList->push_back(&rot1);
modList->push_back(&rot2);
for(int i=0;i < 3;++i) {
cout<<(*modList)[i]->height<<" "<<(*modList)[i]->length<<" "<<(*modList)[i]->width<<" "<<endl;
}
}
return modList;
}
내가 볼 것은 값이 rot1 및 rot2 실제로 이전 값을 덮어 쓸 것입니다 : 프로그램 입력
dimension d[] = {{4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32}};
PVecDim vecdim(new VecDim());
for (int i=0;i<sizeof(d)/sizeof(d[0]); ++i) {
vecdim->push_back(&d[i]);
}
getModList(vecdim);
프로그램입니다. 예를 들어, cout 문은 맨 위에 정의 된 입력 값에 대해 아래와 같이 인쇄합니다. 누군가가 왜이 값들이 덮어 쓰여지고 있는지 말해 줄 수 있습니까? 이 모든 루프 사이클을 무효화 얻을
modList->push_back(&rot1);
: 당신이 이런 종류의 일을 수행 할 때
rot1 7 4 6
rot2 6 7 4
4 7 6
7 4 6
6 7 4
rot1 3 1 2
rot2 2 3 1
4 7 6
3 1 2
2 3 1
그러나 벡터가 틀리거나 새로운 포인터가 될 때마다 포인터 복사본을 저장합니다. 어떤 문제가 있습니까? –
@tariqzafar 문제는 포인터가 아니라 포인터가 가리키는 곳입니다. – juanchopanza
많이 고마워요 :) –