user_1_1_1
user_1_1_1

Reputation: 923

iterating through a vector of struct elements

Here is my code:

#include<iostream>
#include <vector>
#include <stack>

using namespace std;

struct node {
    node *parent;
    int x, y;
    float f, g, h;
    };
node findmin(vector<node> open)
{
    int mini=2000000;
    node iter,mininode;

    std::vector<node>::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;
    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.

    node goal;
    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,temp;
    current=findmin(open);
   // THE EDIT CODE GOES HERE.

    return 0;
}

Somehow the iteration thru all vector elements is not working. My struct is node. open is a vector of node elements. I am trying to iterate thru all the node elements in findmin function. Can correction be suggested along with reason?

EDIT:

Now suppose i want to use this function like this by putting following lines appropriately in main() in above code:

node current,temp;
current=findmin(open);
cout<<current.f;

for(vector<node>::iterator it = open.begin(); it != open.end(); ++it) {
    if(*it==current)
        {
            open.erase(*it);
        }

    }

Why doesn't this work?

Upvotes: 1

Views: 7821

Answers (2)

Nikolay K
Nikolay K

Reputation: 3850

  1. Next time provide more information for your question. You need to be more specific, tell is it compile errors, or runtime and what errors occured.
  2. Fix your findmin function

    node findmin( vector<node> open ) {
        int mini = 2000000;
        node iter, mininode;
    
        std::vector<node>::iterator it;
        for( it = open.begin( ); it != open.end( ); ++it ) {
            if( it->f<mini ) {
                mini = it->f;
                mininode = *it;
            }
        }
        return mininode;
    }
    
  3. Do not pass vector<node>. Pass its reference (or better constant reference). Because in your example vector will be copied. And use const_iterator.

    node findmin( const vector<node>& open ) { /**/ }
    
  4. Do erase in the loop like this

    for (vector<node>::iterator it = open.begin(); it != open.end(); /* ++it*/) {
      if(*it == current) 
         it = open.erase(it);
      else 
         ++it;
    }
    

    But you will need to overload operator==, or write some other condition in if statement.

Upvotes: 1

Oleg Andriyanov
Oleg Andriyanov

Reputation: 5289

I don't see any reason why !open.empty() should evaluate to false, because the "open" vector is not being affected in the loop body. So you have an infinite loop in your main. I think this is the error.

Upvotes: 1

Related Questions