shreyasva
shreyasva

Reputation: 13456

Vectors inside a struct in C++

struct node {
  char name_;
  vector<node*> nbs_;
};

node a, b, c;
  a.name_ = 'a';
  b.name_ = 'b';
  c.name_ = 'c';
  a.nbs_.push_back(&b);
  a.nbs_.push_back(&c);

  for (vector<node*>::iterator i = a.nbs_.begin(); i != a.nbs_.begin(); i++) {
    cout << (*i)->name_;
  }

Why isn't the above code working. It prints nothing. I wanted it to print node b's and c's name.

Upvotes: 1

Views: 783

Answers (1)

Erik
Erik

Reputation: 91270

i != a.nbs_.begin();

should be

i != a.nbs_.end();

Upvotes: 13

Related Questions