我有一个对象数组,其中有一个向量作为类成员(实际上它是一个结构)。结构是:
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());
}
};
我已经将这些对象的数组构建为全局变量:
Cell* cells = new Cell[maxNumberOfCells];
我想在对象内的向量中添加一些整数。
我试过这个(在函数内):
cells[cellId].borderNodes.push_back(node_id);
或者
cells[cellId].getBorderNodes().push_back(node_id);
他们编译得很好,但没有任何东西添加到向量中。正确的方法是如何做到这一点?这是从数据库读取并添加整数的函数。从 db 查询和读取是正确的,所以没有错误。
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();
}
}
}