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 95745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:42:23+00:00 2026-05-10T23:42:23+00:00

What is the correct way of iterating over a vector in C++? Consider these

  • 0

What is the correct way of iterating over a vector in C++?

Consider these two code fragments, this one works fine:

for (unsigned i=0; i < polygon.size(); i++) {     sum += polygon[i]; } 

and this one:

for (int i=0; i < polygon.size(); i++) {     sum += polygon[i]; } 

which generates warning: comparison between signed and unsigned integer expressions.

The unsigned variable looks a bit frightening to me and I know unsigned variables can be dangerous if not used correctly, so – is this correct?

  • 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. 2026-05-10T23:42:23+00:00Added an answer on May 10, 2026 at 11:42 pm

    For iterating backwards see this answer.

    Iterating forwards is almost identical. Just change the iterators / swap decrement by increment. You should prefer iterators. Some people tell you to use std::size_t as the index variable type. However, that is not portable. Always use the size_type typedef of the container (While you could get away with only a conversion in the forward iterating case, it could actually go wrong all the way in the backward iterating case when using std::size_t, in case std::size_t is wider than what is the typedef of size_type):


    Using std::vector

    Using iterators

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

    Important is, always use the prefix increment form for iterators whose definitions you don’t know. That will ensure your code runs as generic as possible.

    Using Range C++11

    for(auto const& value: a) {      /* std::cout << value; ... */ 

    Using indices

    for(std::vector<int>::size_type i = 0; i != v.size(); i++) {     /* std::cout << v[i]; ... */ } 

    Using arrays

    Using iterators

    for(element_type* it = a; it != (a + (sizeof a / sizeof *a)); it++) {     /* std::cout << *it; ... */ } 

    Using Range C++11

    for(auto const& value: a) {      /* std::cout << value; ... */ 

    Using indices

    for(std::size_t i = 0; i != (sizeof a / sizeof *a); i++) {     /* std::cout << a[i]; ... */ } 

    Read in the backward iterating answer what problem the sizeof approach can yield to, though.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Try: alias lock='gnome-screensaver; gnome-screensaver-command --lock' or lock() { gnome-screensaver gnome-screensaver-command… May 11, 2026 at 1:44 pm
  • added an answer Are you running in IIS7 integrated mode? Classic mode of… May 11, 2026 at 1:44 pm
  • added an answer Something like this: \b(?:0x)?\d+\b or this, if you want to… May 11, 2026 at 1:44 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.