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

  • SEARCH
  • Home
  • 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 6326383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:04:49+00:00 2026-05-24T17:04:49+00:00

I usually iterate over a vector that way: for (int i = 0; i

  • 0

I usually iterate over a vector that way:

for (int i = 0; i < myVector.size(); i++) {
    Element* e = myVector[i];
}

But then the compiler usually gives me this warning:

warning: C4018: '<' : signed/unsigned mismatch

So if an int is not right, which type should the index be? vector::size() seems to be of a “size_type” type, but I’d rather use a more meaningful type. Any suggestion?

  • 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-05-24T17:04:50+00:00Added an answer on May 24, 2026 at 5:04 pm

    You should use std::vector<T>::size_type1. Its unsigned integral type. Its usually same as size_t.

    To know the difference between size_type and size_t, see this topic:

    • C++ for-loop – size_type vs. size_t

    1. Similarly, you can use std::string::size_type, std::list<T>::size_type, std::deque<T>::size_type, std::set<T>::size_type, and so on. Almost all standard containers define a nested type called size_type.


    One can argue that you should use iterator instead of index. But I also see that sometime iterator makes the for-loop very wide horizontally, see this :

    for(std::vector<std::vector<std::string> >::iterator it = v.begin(); it != v.end(); ++it)
    {
    }
    

    It doesn’t look. It in fact irritates sometimes. In such situations, some programmers prefers index over iterator.

    In C++0x, iterator has been made more idiomatic. Now you can use iterator without making the syntax cumbersome:

    for(auto it = v.begin(); it != v.end(); ++it)
    {  
    }
    

    Or even better, using range-based for loop:

    for(auto & item : v)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have to iterate over a potentially very large vector of numbers
Ok I think I am having a bad moment. Usually I can iterate over
Usually when I need to fork in C, I do something like this: pid_t
Basically I want to reverse iterate through 2 std::vectors. a layer has a vector
Usually, you make a button with a given size: <button style=width: 120px; height: 40px;>
Usually, when we have to link an interface element to a field of a
Usually I would expect a String.contains() method, but there doesn't seem to be one.
Usually we write this to get the keys and values from a map. Map
Usually, In the scrollview is a tableview or textview, and then We can control
Usually on Firebug, when inspecting an element, I get a list of fonts, and

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.