我在我的程序中有这种情况,我想在std::vector其中swcObject*使用std::iterators.
代码:
class swcObject // abstract class
{
protected:
// inheritable members
// some are pure virtual functions()
// ...
public:
int x, y, width, height;
// getter/setter
private:
// implicit task()
}
std::vector<swcObject*> objects;
void other::OnClickSwapObject()
{
for (auto i = objects.rbegin(); i != objects.rend(); ++i)
{
if ((*i)->isClick(MOUSE_BUTTON_LEFT))
{
std::swap(*i, *objects.begin()); // is this the right syntax to do it?
break;
}
}
}
如果我应该使用std::reverse_iterator和使用std::vector::begin(),我有点困惑std::vector::end(),std::swap(x)因为添加元素std::vector<swcObject*> objects是通过向后(?)。它可以工作,但我的代码对这种事情安全吗?