如何在结构元素的链接向量中添加修饰语
How to add elemnts in Linked vector of structure elements
我有一个名为 grid 的结构:
struct grid {
grid *parent;
int x, y;
float f, g, h;
};
我还创建了一个结构网格的向量:vector<grid> open
下面是用于路径查找的部分代码。我正在尝试创建一个名为 neighbor
的新grid
,在每次迭代中我都会将其添加到 open
向量中,但由于邻居每次迭代都会更改,因此它没有正确添加到open
向量中。什么是更有效的方法?如何添加到open
载体?基本上如何实现结构成员的链接向量而不是链表?
while( !open.empty() )
{
grid current, neighbor;
current=//... minimum-value node in the open vector
for(vector<grid>::iterator it = open.begin(); it != open.end();++it )
if((it->x == current.x) &&(it->y == current.y))
it=open.erase(it);
for(i=0;i<4;i++)
{
neighbor.x=current.x+neighbor_primitves[i][0];
neighbor.y=current.y+neighbor_primitves[i][1];
if( (neighbor.x>=0) && (neighbor.x<=100) && (neighbor.y>=0) && (neighbor.y<=100))
{
neighbor.parent=new grid;
neighbor.parent->x=current.x;
neighbor.parent->y=current.y;
neighbor.g=current.g+1;
neighbor.h=heuristic(neighbor,goal);
neighbor.f=neighbor.g+neighbor.h;
if(isitinNless(neighbor,open))
for(vector<grid>::iterator it = open.begin(); it != open.end(); ++it)
{
if((it->x == neighbor.x) &&(it->y == neighbor.y))
it=open.erase(it);
}
if(!(isitin(neighbor,open)) )
open.push_back(neighbor);
}
}
closed.push_back(current);
}
编辑:
grid* test=goal.parent;
cout<<test->x<<" "<<test->y;
当我在 while 循环的末尾添加以下代码行时,我发现父项没有在 while 循环之外更新,即在 while 循环中,我更新了每个网格成员的父项,但在 while 循环的范围之外,这些更新不会发生,就好像父项不能分配给任何网格成员一样。整个代码:
// A star
#include<iostream>
#include <vector>
#include <cmath>
using namespace std;
struct grid {
grid *parent;
int x, y;
float f, g, h;
};
float heuristic(grid one, grid two)
{
float norm= (one.x-two.x)*(one.x-two.x)+(one.y-two.y)*(one.y-two.y);
return (sqrt(norm));
}
bool isitin(grid &ref, vector<grid> &whole)
{
bool key=false;
if(whole.empty())
return key;
std::vector<grid>::iterator it;
for(it = whole.begin(); it != whole.end(); ++it)
{
if((it->x==ref.x) && (it->y==ref.y))
key=true;
}
return key;
}
bool isitinNless(grid &ref, vector<grid> &whole)
{
bool key=false;
if(whole.empty())
return key;
/*
node iter;
while(!whole.empty())
{
iter=whole.back();
if((iter.x==ref.x) && (iter.y==ref.y))
key=true;
whole.pop_back();
}*/
std::vector<grid>::iterator it;
for(it = whole.begin(); it != whole.end(); ++it)
if((it->x==ref.x) && (it->y==ref.y) && (ref.g < it->g))
key=true;
return key;
}
grid& findmin(vector<grid> &open)
{
int mini=2000000;
grid mininode;
/*
while(!open.empty())
{
iter=open.back();
if(iter.f<mini)
{
mini=iter.f;
}
open.pop_back();
}
*/
std::vector<grid>::iterator it;
for(it = open.begin(); it != open.end(); ++it) {
if(it->f<mini)
{
mini=it->f;
mininode=*it;
}
}
return mininode;
}
int main() {
/*
vector<node*> open;
vector<node*> closed;
node *start=new node;
start->x=50;
start->y=50;
start->f=0;
start->g=0;
start->h=0;// you can take it as zero. works instea of the actual distnace between goal and start.
node *goal=new node;
goal->x=53;
goal->y=50;
goal->f=-1;
goal->g=-1;
goal->h=0;
// put the starting node on the open list
open.push_back(start);
node *current=new node;
current=open.back();
cout<<current->x;
*/
vector<grid> open;
vector<grid> closed;
// neighbor generation scheme
int neighbor_primitves[4][2],i;
neighbor_primitves[0][0]=1; neighbor_primitves[0][1]=0;
neighbor_primitves[1][0]=0; neighbor_primitves[1][1]=1;
neighbor_primitves[2][0]=-1; neighbor_primitves[2][1]=0;
neighbor_primitves[3][0]=0; neighbor_primitves[3][1]=-1;
grid start;
start.parent=NULL;
start.x=50;
start.y=50;
start.f=0;
start.g=0;
start.h=0;// you can take it as zero. works instead of the actual distnace between goal and start.
grid goal;
goal.parent=&start; // just a cosmetic assignment.
goal.x=53;
goal.y=50;
goal.f=-1;
goal.g=-1;
goal.h=0;
// put the starting node on the open list
open.push_back(start);
/*if(isitin(start,open))
cout<<"YO!!!";
if(!open.empty())
cout<<"NO!!!";*/
/*
node current;
current=open.back();
cout<<current.x;
*/
while( !open.empty() )//&& !(findmin(open).x==goal.x) && !(findmin(open).y==goal.y) )
{
grid current, neighbor;
// find the node with the least f on the open list, call it "current"
current=findmin(open);
cout<<open.size();
if(current.x==goal.x && current.y==goal.y)
{
cout<<"Goal!!!n";
break;
}
// pop current off the open list
for(vector<grid>::iterator it = open.begin(); it != open.end(); )
{
if((it->x == current.x) &&(it->y == current.y))
{
it=open.erase(it);
}
else
++it;
}
cout<<open.size();
// generate q's 8 successors and set their parents to current
for(i=0;i<4;i++)
{
neighbor.x=current.x+neighbor_primitves[i][0];
neighbor.y=current.y+neighbor_primitves[i][1];
if( (neighbor.x>=0) && (neighbor.x<=100) && (neighbor.y>=0) && (neighbor.y<=100))
{
neighbor.parent=new grid;
neighbor.parent->x=current.x;
neighbor.parent->y=current.y;
neighbor.g=current.g+1;
neighbor.h=heuristic(neighbor,goal);
neighbor.f=neighbor.g+neighbor.h;
//if(!(isitinNless(neighbor,open)) && !(isitinNless(neighbor,closed)))
// open.push_back(neighbor);
if(isitinNless(neighbor,open))
{
// pop neighbor off the open list
for(vector<grid>::iterator it = open.begin(); it != open.end(); )
{
if((it->x == neighbor.x) &&(it->y == neighbor.y))
{
it=open.erase(it);
}
else
++it;
}
}
if(isitinNless(neighbor,closed))
{
// pop neighbor off the closed list
for(vector<grid>::iterator it = closed.begin(); it != closed.end(); )
{
if((it->x == neighbor.x) &&(it->y == neighbor.y))
{
it=closed.erase(it);
}
else
++it;
}
}
if(!(isitin(neighbor,open)) && !(isitin(neighbor,closed)))
open.push_back(neighbor);
}
}
closed.push_back(current);
// cout<<open.size()<<"n";
cout<<"n";
for(vector<grid>::iterator it = open.begin(); it != open.end(); ++it)
cout<<it->x<<" "<<it->y<<" "<<it->parent->x<<" "<<it->parent->y<<"n";
}
grid* test=goal.parent;
cout<<test->x<<" "<<test->y;
cin.get();
return 0;
}
你的问题出在findmin
.你希望它返回对vector
元素的引用,但它返回对无效数据位置的引用:
grid& findmin(vector<grid> &open)
{
int mini=2000000;
grid mininode; //locally allocated grid
std::vector<grid>::iterator it;
for(it = open.begin(); it != open.end(); ++it) {
if(it->f<mini)
{
mini=it->f;
mininode=*it; //assigns values in the grid pointed to by it to mininode
}
}
return mininode; //returns a reference to the locally scoped grid
} //destroys mininode
您可以清楚地看到这是一个问题。更好的解决方案是将findmin
替换为:
auto& current = *find_min(open.begin(), open.end(), [](const grid& first, const grid& smallest){return first->f < smallest->f;});
相关文章:
- 有没有办法向这个向量添加元素?
- 这种方式是否可以接受向向量添加unique_ptr?
- 向向量添加元素
- 无法向向量添加回调
- 如何将向量添加到结构中以创建一个库存系统,在该系统中,我可以仅使用一个结构向系统添加多种不同的葡萄酒
- 在一个类中向向量添加元素不适用于其他类
- 以下向 c++ 向量添加元素的方法有什么区别
- 我可以在不推送向量的情况下向向量添加值吗?
- 向向量C++添加对
- 使用单词键将多个行号的向量添加到我的地图中
- 在C++中向指针数组中的向量添加元素
- 要创建一个一开始没有特定维度的类Vector,请在类中创建一个方法,允许向向量添加维度
- 一次向 C++ 向量添加多个元素
- STL - 向存储在 STL 映射中的向量添加值
- 向向量添加向量
- 如何向结构类型的向量添加点
- 为什么我的课'当我向向量添加实例时,会调用s析构函数
- 向c++向量添加新字段
- 在c++中向向量添加对象vs向向量添加指针
- 当Type为unique_ptr时,为向量添加运算符[]