这对某人来说肯定很有趣。所以我有一个对象数组(在我的测试/演示代码中,为了简单起见,我只是将它们加倍),然后我创建了一个指向这些对象的指针数组。
我这样做的原因是因为我要交换对象,并且交换对象本身会很昂贵,而交换它们的指针(在辅助数组中)很便宜。
我的盒子数据类型中有许多子索引。基本上,我的目标是能够保持指向盒子“拥有”的对象的连续指针。为此,当我从一个盒子转移到另一个盒子时,我必须在主指针数组中执行多次交换。我通过使用我的盒子结构中的子索引来做到这一点,并且在那里发生了错误......请参阅注释行以了解错误。
#include <iostream>
typedef struct box_s {
double * TestArrayPointer;
} box_t;
main () {
double * TestArray;
double ** TestPointerArray;
TestArray = new double [100];
TestPointerArray = new double * [100];
for (unsigned int Counter = 0; Counter<100; Counter++)
{
TestArray[Counter]=Counter;
TestPointerArray[Counter]=&(TestArray[Counter]);
}
for (unsigned int Counter = 0; Counter<100; Counter++)
std::cout << "P: " << TestPointerArray[Counter] << " V: "
<< *(TestPointerArray[Counter]) << std::endl;
box_t Boxes[10];
for (unsigned int Counter = 0; Counter<10; Counter++)
Boxes[Counter].TestArrayPointer = TestPointerArray[Counter*10];
for (unsigned int Counter = 0; Counter<10; Counter++)
std::cout << "P: " << Boxes[Counter].TestArrayPointer << " V: "
<< *(Boxes[Counter].TestArrayPointer)
<< " P+1: " << (Boxes[Counter].TestArrayPointer)+1 << " V+1: "
<< *((Boxes[Counter].TestArrayPointer)+1)
<< " P: " << Boxes[Counter].TestArrayPointer <<std::endl;
for (unsigned int Counter = 0; Counter<10; Counter++)
std::cout << "P: " << &(Boxes[0].TestArrayPointer[Counter]) << " V: "
<< Boxes[0].TestArrayPointer[Counter] << std::endl;
unsigned int ExitBox = 1;
unsigned int EntranceBox=9;
unsigned int OldIndex = 12;
double * TempPtr = TestPointerArray[OldIndex];
if (ExitBox < EntranceBox)
{
//Swap final element of this box into old position
TestPointerArray[OldIndex] = Boxes[ExitBox+1].TestArrayPointer-1;
//Keep swapping start for end
for(unsigned int BoxNum = ExitBox+1; BoxNum<EntranceBox &&
BoxNum+1<=9;BoxNum++)
{
//Fill in new first with last element of old array.
(Boxes[BoxNum-1].TestArrayPointer-1)=
Boxes[BoxNum+1].TestArrayPointer-1; //darn l-value error!
//Change array head pointer to match.
Boxes[BoxNum].TestArrayPointer=Boxes[BoxNum-1].TestArrayPointer-1;
}
Boxes[EntranceBox].TestArrayPointer = TempPtr;
}
for (unsigned int Counter = 0; Counter<100; Counter++)
std::cout << "P: " << TestPointerArray[Counter] << " V: "
<< *(TestPointerArray[Counter]) << std::endl;
}
我认为这是因为“-1”,但是当我在上面的循环中用“+1”做类似的事情时,我不确定为什么这是无效的。
PS这是真正的测试代码。我编译
g++ main.cpp
. 我没有仔细注意评论和大小写,只是试图坚持MSDN建议的大小写方案,所以不要判断......这只是一个快速&肮脏的测试!
编辑 1
这是任何关心的人的工作代码... PMG 我将其更改为c++
给定new
用法。和希思我给你信用,虽然你的回答没有解决根本问题。
这是真正的解决方案:
#include <iostream>
typedef struct box_s {
double ** TestArrayPointer;
unsigned int NumberPoints;
} box_t;
main () {
double * TestArray;
double ** TestPointerArray;
TestArray = new double [100];
TestPointerArray = new double * [100];
for (unsigned int Counter = 0; Counter<100; Counter++)
{
TestArray[Counter]=Counter;
TestPointerArray[Counter]=&(TestArray[Counter]);
}
for (unsigned int Counter = 0; Counter<100; Counter++)
std::cout << "P: " << TestPointerArray[Counter] << " V: "
<< *(TestPointerArray[Counter]) << std::endl;
box_t Boxes[10];
for (unsigned int Counter = 0; Counter<10; Counter++)
{
Boxes[Counter].TestArrayPointer = TestPointerArray+Counter*10;
Boxes[Counter].NumberPoints=10;
}
for (unsigned int Counter = 0; Counter<10; Counter++)
std::cout << "P: " << Boxes[Counter].TestArrayPointer << " V: "
<< *(Boxes[Counter].TestArrayPointer)
<< " P+1: " << (Boxes[Counter].TestArrayPointer)+1 << " V+1: "
<< *((Boxes[Counter].TestArrayPointer)+1)
<< " P: " << Boxes[Counter].TestArrayPointer <<std::endl;
for (unsigned int Counter = 0; Counter<10; Counter++)
std::cout << "P: " << &(Boxes[0].TestArrayPointer[Counter]) << " V: "
<< Boxes[0].TestArrayPointer[Counter] << std::endl;
unsigned int ExitBox = 1;
unsigned int EntranceBox=9;
unsigned int OldIndex = 12;
double * TempPtr = TestPointerArray[OldIndex];
if (ExitBox < EntranceBox)
{
Boxes[ExitBox].NumberPoints--;
Boxes[EntranceBox].NumberPoints++;
//Swap final element of this box into old position
TestPointerArray[OldIndex] = *(Boxes[ExitBox+1].TestArrayPointer-1);
//Keep swapping start for end
for(unsigned int BoxNum = ExitBox+1; BoxNum<EntranceBox &&
BoxNum+1<=9;BoxNum++)
{
Boxes[BoxNum].TestArrayPointer=
Boxes[BoxNum].TestArrayPointer-1;
//Fill in new first with last element of old array.
*(Boxes[BoxNum].TestArrayPointer)=
*(Boxes[BoxNum+1].TestArrayPointer)-1;
std::cout <<"---------------------" << std::endl;
for (unsigned int Counter = 0; Counter<100; Counter++)
std::cout << "P: " << TestPointerArray[Counter] << " V: "
<< *(TestPointerArray[Counter]) << std::endl;
}
Boxes[EntranceBox].TestArrayPointer=
Boxes[EntranceBox].TestArrayPointer-1;
*(Boxes[EntranceBox].TestArrayPointer) = TempPtr;
}
for(unsigned int BoxNum = 0; BoxNum<=9;BoxNum++)
{
std::cout <<"---------------------" << std::endl;
for(unsigned int Counter = 0; Counter<Boxes[BoxNum].NumberPoints;
Counter++)
std::cout << "P: " <<Boxes[BoxNum].TestArrayPointer[Counter]
<< " V: "
<< *(Boxes[BoxNum].TestArrayPointer[Counter])
<< std::endl;
}
}
注意正确的交换!