使一维指针数组指向二维向量中的对象
Make 1D array of pointers pointing towards an Object in a 2D vector
我不明白哪里出了问题。我正在尝试让一个指针使用另一个指针的地址来访问在开始时创建的对象。
class Soldier{
public:
Soldier(char c){
type = c;
}
char type;
};
//a 2d vector of armies and its soldiers
vector< vector<Soldier> > *armys = new vector< vector<Soldier> >(3, vector<Soldier>(5, Soldier('@')));
//making a pointer array
Soldier **deployment = new Soldier*[3*5];
//test if it works:
//show the original value
cout << "the third soldier of the first army is of type " << (*armys)[1][2].type << endl;
//initializing the pointers of the deployment array to make them point to the Object through the vector.
(*deployment)[1 * (armys->size()) + 2] = (*armys)[1][2];
//show the value via the pointer
cout << "the third soldier of the first army is of type " << (*deployment)[1 * (armys->size()) + 2].type << endl;
上面的代码在显示第二条消息之前触发了分段错误。
为什么不起作用?
在下面的语句中,您将一个 Soldier
对象复制到一个已初始化指针的位置。这只会出错:
(*deployment)[1 * (armys->size()) + 2] = (*armys)[1][2];
如何解决?
deployment
是 3*5 指针的一维数组。所以你必须首先初始化数组元素中的指针(而不是复制对象):
deployment[1 * (armys->size()) + 2] = &(*armys)[1][2]; // take address of armys element
然后你可以通过指针间接引用你的Soldier
对象:
cout << "the third soldier of the first army is of type "
<< deployment[1 * (armys->size()) + 2]->type << endl;
请记住,对于 Soldier **deployment
,deployment[i]
将是指向 Soldier
的指针。
就这些了吗?
虽然更正后的代码有效,但您在一维数组中计算索引的方式可能会在以后给您带来麻烦,因为它不准确。假设你想带走你最后一支军队的最后一名士兵,(*armys)[2][4]
。在您的方案中,您将使用 deployment[2 * armys->size()+4]
。但是 armys->size()
是 3,因此您将采用元素 deployment[10]
而不是 14。在您的索引 "flatening" 中,您应该采用每行的大小而不是行数。假设每支军队的规模始终相同,则为:deployment[i * (*armys)[0].size()+j]
for (*armys)[i][j]
初始化循环因此可以是:
for (int i=0; i<armys->size(); i++)
for (int j=0; j<(*armys)[i].size(); j++)
deployment[i * (*armys)[0].size()+j] = &(*armys)[i][j];
顺便说一下,由于索引从 0 开始,(*armys)[1][2]
将是您的文本输出中第二军的第三名士兵(不是第一名)。
我不明白哪里出了问题。我正在尝试让一个指针使用另一个指针的地址来访问在开始时创建的对象。
class Soldier{
public:
Soldier(char c){
type = c;
}
char type;
};
//a 2d vector of armies and its soldiers
vector< vector<Soldier> > *armys = new vector< vector<Soldier> >(3, vector<Soldier>(5, Soldier('@')));
//making a pointer array
Soldier **deployment = new Soldier*[3*5];
//test if it works:
//show the original value
cout << "the third soldier of the first army is of type " << (*armys)[1][2].type << endl;
//initializing the pointers of the deployment array to make them point to the Object through the vector.
(*deployment)[1 * (armys->size()) + 2] = (*armys)[1][2];
//show the value via the pointer
cout << "the third soldier of the first army is of type " << (*deployment)[1 * (armys->size()) + 2].type << endl;
上面的代码在显示第二条消息之前触发了分段错误。
为什么不起作用?
在下面的语句中,您将一个 Soldier
对象复制到一个已初始化指针的位置。这只会出错:
(*deployment)[1 * (armys->size()) + 2] = (*armys)[1][2];
如何解决?
deployment
是 3*5 指针的一维数组。所以你必须首先初始化数组元素中的指针(而不是复制对象):
deployment[1 * (armys->size()) + 2] = &(*armys)[1][2]; // take address of armys element
然后你可以通过指针间接引用你的Soldier
对象:
cout << "the third soldier of the first army is of type "
<< deployment[1 * (armys->size()) + 2]->type << endl;
请记住,对于 Soldier **deployment
,deployment[i]
将是指向 Soldier
的指针。
就这些了吗?
虽然更正后的代码有效,但您在一维数组中计算索引的方式可能会在以后给您带来麻烦,因为它不准确。假设你想带走你最后一支军队的最后一名士兵,(*armys)[2][4]
。在您的方案中,您将使用 deployment[2 * armys->size()+4]
。但是 armys->size()
是 3,因此您将采用元素 deployment[10]
而不是 14。在您的索引 "flatening" 中,您应该采用每行的大小而不是行数。假设每支军队的规模始终相同,则为:deployment[i * (*armys)[0].size()+j]
for (*armys)[i][j]
初始化循环因此可以是:
for (int i=0; i<armys->size(); i++)
for (int j=0; j<(*armys)[i].size(); j++)
deployment[i * (*armys)[0].size()+j] = &(*armys)[i][j];
顺便说一下,由于索引从 0 开始,(*armys)[1][2]
将是您的文本输出中第二军的第三名士兵(不是第一名)。