sohel14_cse_ju sohel14_cse_ju - 1 year ago 99
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:


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

Can any one suggest about iterator overloading for my struct?

Answer Source


std::vector<Rule>& topPriorityRules;

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


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download