C++ 中的二维向量插入
2d Vector Insertions in C++
目前正在使用向量的向量进行图形表示。我试图在 adjacencies
内的特定位置插入一个边向量。 adjacencies
定义为 adjacencies = new std::vector< std::vector<Edge*>* >;
我 运行 遇到矢量未插入特定 .stateId
位置的问题。逻辑很可能不是我想要的。我需要调整矢量的大小吗?从文档中,我假设向量在当前不在向量中的位置插入时会自动调整大小。感谢您的澄清。
这是我的方法:
/*
* Connecting Edge vertF -----> vertT via weigh
* adjacencies[v][e]
*/
void GraphTable::InsertEdgeByWeight(Vertex* vertF,Vertex* vertT, char weigh){
Edge* tempEdge = new Edge(vertT,weigh);
/*
* Need to figure out how to properly allocate the space in adjacencies.size()
* Test 4 works with initial ID 0 but not test 5 with ID 4
*/
std::vector<Edge*>* temp_vec = new vector<Edge*>;
temp_vec->push_back(tempEdge);
/*if vector at location doesnt exist, we will push a new vector of edges otherwise we
* will need to push the edge into the current vector
*/
if(adjacencies->size()<vertF->thisState.stateId){
adjacencies->resize(vertF->thisState.stateId);
adjacencies[vertF->thisState.stateId].push_back(temp_vec);
}else{
adjacencies[vertF->thisState.stateId].push_back(temp_vec);
}
cout<< adjacencies->capacity() << endl;
//cout<< adjacencies->max_size() << endl;
}
您正在将 adjacencies
的大小调整为 vertF->thisState.stateId
的值,然后调用 adjacencies[vertF->thisState.stateId]
.
如果一个vector/array的大小是"x",那么最高索引就是"x-1"。
所以你应该这样写 -:
adjacencies[vertF->thisState.stateId-1].push_back(temp_vec);
编辑:正如 Ankit Garg 在评论中指出的那样,您应该将 tempEdge
直接推送到 adjacencies
而不是创建临时向量。
从我的评论扩展,我认为你必须做这样的事情:
if(adjacencies->size() < vertF->thisState.stateId)
{
// the vector does not exist, hence push_back the new vector
.....
}
else
{
// vector already exists, so just push_back the edge
adjacencies[vertF->thisState.stateId].push_back(temp_edge);
}
目前正在使用向量的向量进行图形表示。我试图在 adjacencies
内的特定位置插入一个边向量。 adjacencies
定义为 adjacencies = new std::vector< std::vector<Edge*>* >;
我 运行 遇到矢量未插入特定 .stateId
位置的问题。逻辑很可能不是我想要的。我需要调整矢量的大小吗?从文档中,我假设向量在当前不在向量中的位置插入时会自动调整大小。感谢您的澄清。
这是我的方法:
/*
* Connecting Edge vertF -----> vertT via weigh
* adjacencies[v][e]
*/
void GraphTable::InsertEdgeByWeight(Vertex* vertF,Vertex* vertT, char weigh){
Edge* tempEdge = new Edge(vertT,weigh);
/*
* Need to figure out how to properly allocate the space in adjacencies.size()
* Test 4 works with initial ID 0 but not test 5 with ID 4
*/
std::vector<Edge*>* temp_vec = new vector<Edge*>;
temp_vec->push_back(tempEdge);
/*if vector at location doesnt exist, we will push a new vector of edges otherwise we
* will need to push the edge into the current vector
*/
if(adjacencies->size()<vertF->thisState.stateId){
adjacencies->resize(vertF->thisState.stateId);
adjacencies[vertF->thisState.stateId].push_back(temp_vec);
}else{
adjacencies[vertF->thisState.stateId].push_back(temp_vec);
}
cout<< adjacencies->capacity() << endl;
//cout<< adjacencies->max_size() << endl;
}
您正在将 adjacencies
的大小调整为 vertF->thisState.stateId
的值,然后调用 adjacencies[vertF->thisState.stateId]
.
如果一个vector/array的大小是"x",那么最高索引就是"x-1"。
所以你应该这样写 -:
adjacencies[vertF->thisState.stateId-1].push_back(temp_vec);
编辑:正如 Ankit Garg 在评论中指出的那样,您应该将 tempEdge
直接推送到 adjacencies
而不是创建临时向量。
从我的评论扩展,我认为你必须做这样的事情:
if(adjacencies->size() < vertF->thisState.stateId)
{
// the vector does not exist, hence push_back the new vector
.....
}
else
{
// vector already exists, so just push_back the edge
adjacencies[vertF->thisState.stateId].push_back(temp_edge);
}