I want to modify vector
stack entries in while
loop, but somehow reference modification does not work. I thin i am modifying copied one, but i can't figure out how to modify real entry.
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
int main() {
struct Node {
Node* parent;
std::vector<std::unique_ptr<Node> > children;
Node(Node* parent) : parent(parent){};
};
struct Tree {
std::unique_ptr<Node> root;
Tree():root(new Node{nullptr}){};
};
std::unique_ptr<Tree> t{new Tree};
auto p = t->root.get();
p->children.emplace_back(new Node{p});
p->children.emplace_back(new Node{p});
p = p->children[0].get();
p->children.emplace_back(new Node{p});
p = p->children[0].get();
p->children.emplace_back(new Node{p});
p->children.emplace_back(new Node{p});
using Сhildren_const_iterator = decltype(std::cbegin(t->root->children));
struct FirstCurrentLast {
const Сhildren_const_iterator first;
Сhildren_const_iterator current;
const Сhildren_const_iterator last;
};
std::vector<FirstCurrentLast> stack;
stack.push_back({std::cbegin(t->root->children),
std::cbegin(t->root->children),
std::cend(t->root->children)});
auto print_stack = [&stack]() {
std::cout << "/stack.size= " << stack.size() << "\n";
std::string row1, row2, row3;
for (auto&& it : stack) {
row1.append(std::to_string(0)).append("_.");
row2.append(std::to_string(it.current - it.first)).append("_.");
row3.append(std::to_string(it.last - it.first)).append("_.");
};
std::cout << "first |" << row1 << ";\n";
std::cout << "current|" << row2 << ";\n";
std::cout << "last |" << row3 << ";\n\n";
};
while (!stack.empty()) {
FirstCurrentLast& iterators = stack.back();
Сhildren_const_iterator first = iterators.first;
Сhildren_const_iterator& current = iterators.current;
Сhildren_const_iterator last = iterators.last;
std::cout << ">begin \n";
print_stack();
if (current == last) {
stack.pop_back();
continue;
};
if (!current->get()->children.empty()) {
print_stack();
stack.push_back({std::cbegin(current->get()->children),
std::cbegin(current->get()->children),
std::cend(current->get()->children)});
print_stack();
};
iterators.current++;
print_stack();
std::cout << "end<\n\n";
abort();
};
std::cout << "fini ok\n";
}
I get output:
>begin
/stack.size= 1
first |0_.;
current|0_.;
last |2_.;
/stack.size= 1
first |0_.;
current|0_.;
last |2_.;
/stack.size= 2
first |0_.0_.;
current|0_.0_.;
last |2_.1_.;
/stack.size= 2
first |0_.0_.;
current|0_.0_.;
last |2_.1_.;
end<
But it should look like this:
>begin
/stack.size= 1
first |0_.;
current|0_.;
last |2_.;
/stack.size= 1
first |0_.;
current|0_.;
last |2_.;
/stack.size= 2
first |0_.0_.;
current|1_.0_.;
last |2_.1_.;
/stack.size= 2
first |0_.0_.;
current|1_.0_.;
last |2_.1_.;
end<