#include <iostream> #include <conio.h> #include <vector> #include <stdio.h> using namespace std; struct treem { char node[5]; char parent[5]; int number; }; vector <treem> CreateAtree() { char value[5]; char parn[5]; int num; vector<treem> tree; treem g; printf("Input the name of the root\n"); scanf("%s",&value); printf("Input the value of node\n"); scanf("%d", &num); tree.push_back(g); strcpy(tree.at(0).parent,"NO"); strcpy(tree.at(0).node, value); tree.at(0).number = num; int i = 1; int flag = 0; while (num != 0 && parn[0] != '0') { printf("Input the name of node\n"); scanf("%s",&value); printf("Input the parent of node\n"); scanf("%s",&parn); printf("Input the value of node\n"); scanf("%d",&num); for (vector<treem>::iterator it = tree.begin(); it != tree.end(); it++) { if (strcmp(parn, it->node) == 0) flag = 1; if (flag == 1) { tree.push_back(g); strcpy(tree.at(i).parent, parn); strcpy(tree.at(i).node, value); tree.at(i).number = num; i++; flag = 0; } else printf("There is not such parent,please try again\n"); } } return tree; } int main() { vector<treem> tree = CreateAtree(); _getch(); return 0; } 

I do something like a tree. I create a root then when I add a new vertex and indicate the root (my verification is there) knocks out the message vector iterator not incrementable. I work with vector for the first time therefore I ask for advice how to fix it.

    1 answer 1

    This loop does not make sense, since inside the loop elements are added to the vector, because iterators are not valid, as the vector can redefine the memory to accommodate new elements.

     for (vector<treem>::iterator it = tree.begin(); it != tree.end(); it++) { if (strcmp(parn, it->node) == 0) flag = 1; if (flag == 1) { tree.push_back(g); ^^^^^^^^^^^^^^^^^ 

    What you need is to use the std::find_if or std::any_of instead of a loop, and if the parent element is found, then add a new element to the vector, then use the member function of the back class to change the values ​​of this added element.

    In addition, in your tree there is no connection between the nodes of the tree.

    The following shows how you can rewrite your code to at least work.

     #include <iostream> #include <vector> #include <iterator> #include <algorithm> #include <limits> #include <cstring> const size_t N = 5; struct treem { char node[N]; char parent[N]; int number; }; std::vector<treem> CreateAtree() { std::vector<treem> tree; treem node; std::cout << "Input the name of the root: "; std::cin.getline(node.node, N); std::cout << "Input the value of node: "; std::cin >> node.number; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::strcpy(node.parent, "NO"); tree.push_back(node); while (true) { std::cout << "Input the parent of the next node: "; if ( !std::cin.getline(node.parent, N) || node.parent[0] == '\0') break; if (std::any_of(tree.begin(), tree.end(), [&](const treem &t) { return std::strcmp(t.node, node.parent) == 0; })) { std::cout << "Input the name of the next node: "; std::cin.getline(node.node, N); std::cout << "Input the value of the next node: "; std::cin >> node.number; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); tree.push_back(node); } else { std::cout << "There is no such parent,please try again\n"; } } std::cout << std::endl; return tree; } int main() { std::vector<treem> tree = CreateAtree(); for (const treem &node : tree) { std::cout << "parent: " << node.parent << ", name: " << node.node << ", value: " << node.number << std::endl; } return 0; } 

    The dialogue with the program may look like this.

     Input the name of the root: A Input the value of node: 0 Input the parent of the next node: A Input the name of the next node: B Input the value of the next node: 1 Input the parent of the next node: A Input the name of the next node: C Input the value of the next node: 2 Input the parent of the next node: B Input the name of the next node: D Input the value of the next node: 3 Input the parent of the next node: parent: NO, name: A, value: 0 parent: A, name: B, value: 1 parent: A, name: C, value: 2 parent: B, name: D, value: 3 
    • what needs to be changed so that this code will work as I want, for the sake of interest, I want this code to work. - toshka-pitoshka
    • @ toshka-pitoshka I must rewrite the code according to my answer. - Vlad from Moscow