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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:51:56+00:00 2026-06-17T11:51:56+00:00

In modern C++, the idiom for iterating a sequential collection like string or vector

  • 0

In modern C++, the idiom for iterating a sequential collection like string or vector when you only need the value of each element is short and elegant:

for (auto x: xs)

When you also need the index, it’s a little less elegant:

for (size_t i = 0; i != xs.size() ++i)

… unless there’s some recent development I haven’t yet caught up with. Does C++11 have a preferred way of doing the latter, or is the above still as good as it gets?

  • 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-17T11:51:57+00:00Added an answer on June 17, 2026 at 11:51 am

    The preferred and idiomatic way is the simple for loop.

    Alternative methods include using an integer range:

    template<typename C>
    auto container_index(C const& container) -> decltype(boost::irange(0, container.size())) {
      return boost::irange(0, container.size());
    }
    
    for(auto x : container_index(xs))
    

    Or an iterating function:

    template<typename F>
    void index_iterate(std::size_t size, F func) {
      for(std::size_t i = 0; i != size; ++i) {
        func(i);
      }
    }
    
    index_iterate(container.size(), [&](std::size_t i){ /* ... */ });
    

    Just go with the simple loop whenever possible though. It’s superior in my opinion.

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

Sidebar

Related Questions

use Modern::Perl; use Algorithm::Permute; use List::AllUtils qw/uniq/; find_perms(1151); sub find_perms { my ($value) =
For each of the modern browsers are there well known hooks I can tie
Modern hardware-assisted desktop virtualization products (like VMWare Workstation or VirtualBox) normally provide the guest
The question is for any modern unmanaged language.C-like languages,delphi,anything. I'd like to create my
JSON looks like this: [{title:Modern\/Contemporary House of mine.,link:http:\/\/buildworx-mc.com\/forum\/showthread.php?tid=1718,images:[http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/house1.png,http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House2.png,http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House3.png,http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House4.png,http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House5.png,http:\/\/i1139.photobucket.com\/albums\/n555\/xDJBOUTIx\/House6.png]} I can get the title and
In the Modern Objective-C runtime, you can do something like this: @interface MyClass :
These days modern sites are becoming more and more service oriented like facebook/gmail. A
To what extent are modern C++ features like: polymorphism, STL, exception safety/handling, templates with
The codebases for modern web browsers like Chrome, Firefox, and Safari (WebKit) are quite
In Linux or other modern OS, each process's memory is protected, so that 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.