- #1
exitwound
- 292
- 1
I'm trying to work on a project for class (that's not homework but a long-term assignment, second c++ semester so I'm not very bright yet) and I'm having trouble figuring out why the iterator of this vector won't work as I hoped.
Here's the structure of the vector(s) so far:
CarList is a vector<Auto>.
carToModify is the element of the CarList vector I'm working on.
Auto is a class with the following:
string carName;
string carPurchaseDate;
double carCost;
string randomUID;
vector<Part> PartsList;
Part is a class that has the same type of information (name, purchase date etc) but I don't think that's relevant to this.
I display the list of parts on the car and I want to delete one of them (say, the one at the first position in PartsList vector (index=0)). In order to do that, I need an iterator pointing to the first position in that PartsList vector, correct?
.Get_Parts_List() returns PartsList.
And once I have the iterator, I can use .erase(iterator) to erase that value in the vector, correct? (subsequently shrinking the vector down by 1 and shifting the indices of the other parts).
I get vector out of bounds errors when I do this though.
Can you help?
Here's the structure of the vector(s) so far:
CarList is a vector<Auto>.
carToModify is the element of the CarList vector I'm working on.
Auto is a class with the following:
string carName;
string carPurchaseDate;
double carCost;
string randomUID;
vector<Part> PartsList;
Part is a class that has the same type of information (name, purchase date etc) but I don't think that's relevant to this.
I display the list of parts on the car and I want to delete one of them (say, the one at the first position in PartsList vector (index=0)). In order to do that, I need an iterator pointing to the first position in that PartsList vector, correct?
Code:
vector<Part>::iterator it = (carList[carToModify].Get_Parts_List()).begin();
.Get_Parts_List() returns PartsList.
And once I have the iterator, I can use .erase(iterator) to erase that value in the vector, correct? (subsequently shrinking the vector down by 1 and shifting the indices of the other parts).
Code:
(carList[carToModify].Get_Parts_List()).erase(it);
I get vector out of bounds errors when I do this though.
Can you help?