Comparing signed and unsigned values

Posted on 2005-04-21
Last Modified: 2010-04-01
Hi experts,

   How do I get rid of the following warning ?
  line 89 :  for(int i=0; i< group_1.size(); i++)

[C++ Warning] Meow.cpp(89): W8012 Comparing signed and unsigned values

 many thanks !
Question by:meow00
    LVL 12

    Expert Comment

    for ( unsigned int i = 0 .....

    LVL 16

    Expert Comment

    or for(int i=0; i< (int)group_1.size(); i++)
    LVL 12

    Expert Comment

    Or, really, know what the correct tyoe of group_1.size() is, and use that for your index...

    What's group_1 ?
    LVL 30

    Assisted Solution

    Hi meow00,
    >> for(int i=0; i< group_1.size(); i++)

    If group_1 is an STL container, then you should be using size_t.

    for(size_t i=0; i< group_1.size(); ++i)

    David Maisonave :-)
    LVL 86

    Accepted Solution

    If group_1 is an STL container, you could also use iterators, e.g.:

    for ( [container]::iterator i = group_1.begin(); i != group_1.end(); ++i) {


    Another thing (a bit nitpicking)  - in all the loop variations, take 'group_1.end()' and 'group_1.size()' out of the loop, unless that value is not constant.
    LVL 12

    Expert Comment

    I feel somewhat cheated!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    Some Windows API functions expect you to provide a pointer to a CALLBACK function that the system will need to call as part of the operation.  Such API functions as SetTimer, timeSetEvent, CreateThread, EnumWindows, LineDDA, even window message hand…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now