size of vector made of struct
Dec 1, 2016 at 4:12pm UTC
I am having trouble find the size of the vector because it is made of a struct. My program is looking at the total size of the vector and going too far. How can I find the size of the vector for just one part of the struct and not the entire vector? Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
string lowestCost(const vector <parts> &pVector)
{
string part;
int lowPos=0;
double lowCost, temp;
lowCost = pVector[lowPos].ohb * pVector[lowPos].cost;
for (int count = 1; count < pVector.size(); count++)
{
temp = pVector[count].ohb * pVector[count].cost;
if (temp < lowCost)
{
lowCost = temp;
lowPos = count;
}
}
part = pVector[lowPos].number;
return part;
}
Dec 1, 2016 at 4:38pm UTC
I am having trouble find the size of the vector because it is made of a struct.
That should make no difference.
My program is looking at the total size of the vector and going too far.
I suspect your problem is occurring at line 7. If the vector is empty, lowPos (0) will be out of bounds.
Your for loop at line 10 looks fine.
How can I find the size of the vector for just one part of the struct and not the entire vector?
That makes no sense. pVector.size() returns the number of elements (structs) in the vector. That is what you want.
Last edited on Dec 1, 2016 at 4:39pm UTC
Dec 1, 2016 at 5:13pm UTC
found a way around it instead of count < pVector.size()
I used count < (pVector.size()/4)
Dec 1, 2016 at 9:21pm UTC
Please explain why you think you need to divide pVector.size() by 4?
Again, that makes no sense.
Topic archived. No new replies allowed.