sohel14_cse_ju sohel14_cse_ju - 20 days ago 8
C++ Question

Remove first item of vector

Here is my type :

struct Rule
{
int m_id = -1;
std::wstring name;
double angle;
};


Vector of that type :

std::vector<Rule>& topPriorityRules;


I am trying to erase first element:

topPriorityRules.erase(topPriorityRules.begin());


But can't do it. Looks like i need to do iterator overloading.

Can any one suggest about iterator overloading for my struct?

Answer

Given

std::vector<Rule>& topPriorityRules;

The correct way to remove the first element of the referenced vector is

topPriorityRules.erase(topPriorityRules.begin());

which is exactly what you suggested.

Looks like i need to do iterator overloading.

There is no need to overload an iterator in order to erase first element of std::vector.


P.S. Vector (dynamic array) is probably a wrong choice of data structure if you intend to erase from the front.