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++)
{
...
}
LVL 1
shaolinfunkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
if vec is the vector of struct, then vec.size() is the number of structs in your vector
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DonConsolioCommented:
Use vector::size  (Returns the number of elements in the vector)

size_type size( ) const;

count = my_vector.size( );
0
shaolinfunkAuthor Commented:
Phoffric strikes again!
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

phoffricCommented:
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/
0
shaolinfunkAuthor Commented:
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

0
shaolinfunkAuthor Commented:
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?
0
phoffricCommented:
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.

==================
0
shaolinfunkAuthor Commented:
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!
0
phoffricCommented:
What compiler you are using? If it is VS, then just note the header when you do a menu Help -> About.
0
shaolinfunkAuthor Commented:
i'm using Microsoft Visual Studio 2005, Version 8.0.50727.42
0
phoffricCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.