Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8457055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:42:34+00:00 2026-06-10T12:42:34+00:00

I could use a little help clarifying this strange comparison when dealing with vector.size()

  • 0

I could use a little help clarifying this strange comparison when dealing with vector.size() aka size_type

vector<cv::Mat> rebuiltFaces;
int rebuildIndex = 1;
cout << "rebuiltFaces size is " << rebuiltFaces.size() << endl;

while( rebuildIndex >= rebuiltFaces.size() ) {
    cout << (rebuildIndex >= rebuiltFaces.size()) << " , " << rebuildIndex << " >= " << rebuiltFaces.size() << endl;
    --rebuildIndex;
}

And what I get out of the console is

rebuiltFaces size is 0
1 , 1 >= 0
1 , 0 >= 0
1 , -1 >= 0
1 , -2 >= 0
1 , -3 >= 0

If I had to guess I would say the compiler is blindly casting rebuildIndex to unsigned and the +- but is causing things to behave oddly, but I’m really not sure. Does anyone know?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:42:35+00:00Added an answer on June 10, 2026 at 12:42 pm

    As others have pointed out, this is due to the somewhat
    counter-intuitive rules C++ applies when comparing values with different
    signedness; the standard requires the compiler to convert both values to
    unsigned. For this reason, it’s generally considered best practice to
    avoid unsigned unless you’re doing bit manipulations (where the actual
    numeric value is irrelevant). Regretfully, the standard containers
    don’t follow this best practice.

    If you somehow know that the size of the vector can never overflow
    int, then you can just cast the results of std::vector<>::size() to
    int and be done with it. This is not without danger, however; as Mark
    Twain said: “It’s not what you don’t know that kills you, it’s what you
    know for sure that ain’t true.” If there are no validations when
    inserting into the vector, then a safer test would be:

    while ( rebuildFaces.size() <= INT_MAX
            && rebuildIndex >= (int)rebuildFaces.size() )
    

    Or if you really don’t expect the case, and are prepared to abort if it
    occurs, design (or find) a checked_cast function, and use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could use a little help. I got this program to work right then
I could use a little help trouble shooting this problem. When using the app
I could use a little help on a MySQL database issue I am dealing
I'am new to Python 3 and could really use a little help. I have
I could use a bit of help with this, as i am new to
I could use a little help in AppEngine land... Using the [Python] API I
I could use a little help with an XPath-Expression. check the code below; how
I'm having some trouble writing a jQuery function and could use a little help.
I am new to XHTML and I could use a little bit of help.
I'm writing my first rails plugin and could use a little help. In a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.