这是我当前的代码。有没有更好的方法在 C++ 中做同样的事情?
{
// create more room in array
Point *temp = new Point[pointList.getSize()*ENLARGE_TIMES];
memcpy(temp,pointList._pointList,sizeof(Point)*pointList.getSize());
pointList.~PointList();
pointList._pointList = temp;
pointList.setSize(pointList.getSize()*ENLARGE_TIMES);
pointList._pointList[iterator] = point;
iterator++;
pointsCounter++;
continue;
}
编辑:不能使用 vector
最佳答案
正如 Chris 指出的那样,您真的想使用 std::vector 以及方法 reserve() 和 push_back(),如下所示:
vector<Point> pointList; // could be a member variable
pointList.reserve(INITIAL_CAPACITY); // up to you
pointList.push_back(point); // adding new elem
pointList.end(); // replaces your iterator
// Some idiomatic iteration code to go with it:
for( auto iter = begin(pointList); iter != end(pointList); ++iter )
{
Point p = *iter; // deref iterator and use result
}
关于c++将数组大小扩大两倍的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16269413/