Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 5 characters in body
Source Link
Caleth
  • 66k
  • 2
  • 53
  • 101

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d&d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_index = 0; d.nodes->at(0).val = 200; cout << d.cur_node().val << endl;; cout << d.nodes->at(0).val << endl;; } 

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_index = 0; d.nodes->at(0).val = 200; cout << d.cur_node().val << endl;; cout << d.nodes->at(0).val << endl;; } 

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = &d.nodes->at(0); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_index = 0; d.nodes->at(0).val = 200; cout << d.cur_node().val << endl;; cout << d.nodes->at(0).val << endl;; } 
added 299 characters in body
Source Link
Caleth
  • 66k
  • 2
  • 53
  • 101

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } };   int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_index = 0; d.nodes->at(0).val = 200; cout << d.cur_node().val << endl;; cout << d.nodes->at(0).val << endl;; } 

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; 

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } };   int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_index = 0; d.nodes->at(0).val = 200; cout << d.cur_node().val << endl;; cout << d.nodes->at(0).val << endl;; } 
added 136 characters in body
Source Link
Caleth
  • 66k
  • 2
  • 53
  • 101

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; 

You can still use Node *.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; 

You can still use Node *, however you need to be careful of the vector's pointer invalidation rules.

struct Foo { unique_ptr<vector<Node>> nodes; Node * cur_node; }; int main() { Foo foo; foo.nodes = make_unique<vector<Node>>(); foo.nodes->push_back((Node){.val=100}); foo.cur_node = d.nodes->at(0).get(); d.nodes->at(0).val = 200; cout << d.cur_node->val << endl;; cout << d.nodes->at(0).val << endl;; } 

Why not just hold the index?

struct Foo { unique_ptr<vector<Node>> nodes; size_t cur_index; Node & cur_node() { return nodes->at(cur_index); } }; 
added 444 characters in body
Source Link
Caleth
  • 66k
  • 2
  • 53
  • 101
Loading
Source Link
Caleth
  • 66k
  • 2
  • 53
  • 101
Loading