size of vector made of struct

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;
}
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
found a way around it instead of count < pVector.size() I used count < (pVector.size()/4)
Please explain why you think you need to divide pVector.size() by 4?

Again, that makes no sense.

Topic archived. No new replies allowed.