这是一个肯定会对某人有趣的人。所以我有一个对象数组(在我的测试/演示代码中,为了简单起见,我只是将它们设为双精度)然后我创建了一个指向这些对象的指针数组。
我这样做的原因是因为我要交换对象,交换对象本身会很昂贵,而交换指针(在辅助数组中)则很便宜。
我的盒子数据类型中有很多子索引。基本上,我的目标是能够保持指向框“拥有”的对象的连续指针。为此,当我从一个盒子转移到另一个盒子时,我必须在主指针阵列中执行多次交换。我通过在我的盒子结构中使用子索引来做到这一点,并且它出现错误...请参阅注释行以获取错误。
#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”做类似的事情时,我不确定为什么这是无效的。
P.S。这是REAL测试代码。我用
编译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;
}
}
请注意正确的交换!!
答案 0 :(得分:3)
我不会质疑你的代码,因为我无法真正阅读它。
关于l值错误,请更改:
(Boxes[BoxNum-1].TestArrayPointer-1) = ...
到
*(Boxes[BoxNum-1].TestArrayPointer-1) = ...
甚至
Boxes[BoxNum-1].TestArrayPointer[-1] = ...
计算指针不是l值,必须取消引用。换句话说,您无法通过更改地址来存储任何内容,您必须将值存储在地址。