Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

How do I find out how many structs my vector contains?

From the main() function I am sending a vector of structs into a function that performs calculations on the members of the struct.

I will use a For loop to iterate through the vector of structs.  I don't know in advance how many structs the vector contains.  How do I find this out so that I can compose my for loop statement such that X is the number of structs contained in the vector?

For(count=1; count<=X,count++)
{
...
}
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use vector::size  (Returns the number of elements in the vector)

size_type size( ) const;

count = my_vector.size( );
Avatar of shaolinfunk

ASKER

Phoffric strikes again!
Avatar of phoffric
phoffric

See vector::size reference:
    http://www.cplusplus.com/reference/stl/vector/size/

Note that size() returns a size_type data type
Here is a claim that size_type is an unsigned integer
    http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html

Here is a discussion on size_type
   http://www.cplusplus.com/forum/beginner/15959/
Ok phoffric I know those links you sent has something to do with my newfound problem.  Why do I get this warning after compiling?

vFakeData is my vector...and I use vFakeData.size to be my upper limit in my for loop.

//iterate through the rest of the array
      for (x = 1; x <= vFakeData.size(); x++)
      {
            ....
      }
1>Compiling...
1>Junk.cpp
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(172) : warning C4018: '<=' : signed/unsigned mismatch
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(191) : warning C4267: '=' : conversion from 'size_t' to 'long', possible loss of data
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\junk\junk\Debug\BuildLog.htm"
1>junk - 0 error(s), 2 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Open in new window

i made x as unsigned int. and it got read of one of the warnings.

but the other warning still persists:

1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(191) : warning C4267: '=' : conversion from 'size_t' to 'long', possible loss of data

the above warning is referring to this line:

longCount = longTotal / vFakeData.size()

I am dividing a long by  a size_t....how do i get rid of this error?
If you break at your for (x = 1; x <= vFakeData.size(); x++) statement and then F11, you will see that size() returns a size_type value. size_type is mentioned in previous post. My book says that size_type is usually implemented as size_t.
    http://www.cplusplus.com/reference/clibrary/cstddef/size_t/

The definition of size_t is implementation dependent (but it must be unsigned integral type).

So, this suggest that x should not be defined as unsigned int but instead defined as size_t. (Even though that seems to work for you now; it is not necessarily portable to a different platform.)

This compiles without warning for me (Level 3):
   size_t longTotal = 15;
   size_t longCount;
   longCount = longTotal / vFakeData.size();

However, if I replace both size_t with an int, it still compiles without warning. So, possibly one of our compilers is not following the conversion rules.

The rule is that "if either binary operand is unsigned long, the other is converted to unsigned long".
So, that implies that the numerator longTotal should get converted to an unsigned long and the division produces an unsigned long. I'm guessing that your longCount is not unsigned long, in which case your warning makes sense. So, I think it is both of my compilers that are incorrect in not generating your warnings.

I currently use Visual Studio 2008 Express and cygwin gcc 4.3.4-3.

Just for reference, please note the exact compiler you are using. If it is VS, then just note the header when you do a menu Help -> About.

==================

As noted in earlier question, sometimes I do not get a warning when you do get a warning. My Project Properties (C/C++ -> General -> Warning Level) is set to Level 3 (/W3). Even if I set it to Level 4, I get no warnings in the area you have (although I do get three warnings about int -> unsigned short starting at:
    year = atoi(&str[0]); )

BTW - For portability, you should add #include <stdlib.h> to get the atoi prototype defined. (Some compilers give a fatal error if you do not.) This, of course, doesn't remove the legitimate level 4 warning.

==================
ahh phoffric...thank you very much for explaining.  i'm not certain that i understand all of it, but i get what you're saying about the size_t.  thanks again!
What compiler you are using? If it is VS, then just note the header when you do a menu Help -> About.
i'm using Microsoft Visual Studio 2005, Version 8.0.50727.42
Was that Express? I actually have a 2005 Standard Edition that MS sent me as a reward in watching two of their virtual labs. Maybe I should open the package and install it. Then again, there's VS2010 Express beta available now. I haven't compared the different versions to see how later is better. (Maybe in the SQL area?)

One thing VS Express did in debugging is to hide some valuable features such as Memory 1,2,3,4 and Set PC pointer to a different line. I had to find these features and add them to the debug toolbar.