I have an array of objects that have a vector as a class member (actually it is a struct). The struct is:
struct Cell { vector<int> borderNodes; vector<int> helperNodes; int minNodePointer; int maxNodePointer; Cell() { minNodePointer = -1; maxNodePointer = -1; } Cell(int _minNodePointer, int _maxNodePointer) { minNodePointer = _minNodePointer; maxNodePointer = _maxNodePointer; } vector<int>& getHelperNodes() { return helperNodes; } vector<int>& getBorderNodes() { return borderNodes; } void setHelperNodesArray() { sort(helperNodes.begin(), helperNodes.end()); } void setBorderNodesArray() { sort(borderNodes.begin(), borderNodes.end()); } }; I have built an array of those objects as a global variable with :
Cell* cells = new Cell[maxNumberOfCells]; and I want to add some integers to the vectors inside the objects.
I tried this (inside a function):
cells[cellId].borderNodes.push_back(node_id); or
cells[cellId].getBorderNodes().push_back(node_id); and they compile fine, but nothing is added to the vector. How is the correct way to do this? Here is the function that reads from a db, and adds the integers. The query and reading from db is correct, so no mistakes there.
void loadBorderNodesPerCellBnOnly(bool shouldRearrangeNodes, int subtractor, int maxNumberOfCells, int** cellBorderNodes) { cellBorderNodes = new int*[maxNumberOfCells]; try { work W(Conn); for (int rownum = 0; rownum < r.size(); ++rownum) { const result::tuple row = r[rownum]; vector<string> s = split(row[1].as<string > (), ' '); const int mySize = s.size(); int cellId = row[0].as<int> (); cellBorderNodes[cellId] = new int[mySize]; for (int k = 0; k < mySize; k++) { int node_id = atoi(s[k].c_str()); cellBorderNodes[cellId][k] = node_id; (cells[cellId].getBorderNodes()).push_back(node_id); try { nodes[node_id - subtractor].cellId = cellId; } catch (const std::exception &e) { std::cerr << e.what() << std::endl; } nodes[node_id - subtractor].isBorderNode = true; } s.clear(); } } catch (const std::exception &e) { std::cerr << e.what() << std::endl; } for (int k = 0; k < maxNumberOfCells; k++) { cout << k << ": " << cells[k].borderNodes.size() << endl; cells[k].setBorderNodesArray(); if (maxBorderNodesPerCell < cells[k].borderNodes.size()) { maxBorderNodesPerCell = cells[k].borderNodes.size(); } } }
push_backand then output the sizes?