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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:14:10+00:00 2026-06-10T01:14:10+00:00

The book i am reading offers this example when iterating over a vector for

  • 0

The book i am reading offers this example when iterating over a vector

for (auto &e: v) {
  cout << e << endl;
}

Suppose v is declared as vector<int> v, in other words, we know that the type of elements inside this collection is int.

Is using auto in any way better or preferred to?

for (int &e: v) {
  cout << e << endl;
}

Why?

  • 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-10T01:14:11+00:00Added an answer on June 10, 2026 at 1:14 am

    Yes. auto is preferred. Because if you change the declaration ofv from:

    std::vector<int> v;  //before 
    

    to this:

    std::vector<float> v; //after
    

    If you use int & in the for, then you have to change that as well. But with auto, no need to change!

    In my opinion, working with auto is more or less like programming to interface. So if you do an operation += in the loop, and you don’t really care about the type of the loop variable e as long as the type supports += operation, then auto is the solution:

    for(auto & e : v)
    {
          e += 2;
    }
    

    In this example, all you care about that the type of e supports += with int on the right hand side. It will work even for user-defined types, which has defined operator+=(int), or operator+=(T) where T is a type which supports implicit conversion from int . It is as if you’re programming to interface:

    std::vector<Animal*> animals;
    animals.push_back(new Dog());
    animals.push_back(new Cat());
    animals.push_back(new Horse());
    
    for(size_t i = 0 ; i < animals.size(); ++i)
    {
           animals[i]->eat(food); //program to interface
    }
    

    Of course, you would like to write this loop as:

    for(Animal * animal : animals)
    {
           animal->eat(food); //still program to interface
    }
    

    Or simply this:

    for(auto animal : animals)
    {
           animal->eat(food); //still program to interface
    }
    

    It is still programming to interface.

    But at the same time, the point in @David’s comment is worth noting.

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

Sidebar

Related Questions

I'm reading a book to read and it covers this below example. somelist =
I'm reading Jon Skeet's book and he offers a great example: List<Product> products =
Came across this example in the book im reading and it didn't make sense
While reading a book about JavaScript I stumbled across an example: var names =
I'm reading the book 'Beginning F#', There's a short list for example code, to
I am reading a book in which author used a code like this public
I'm reading this book on C# and .NET and I'm learning a bunch of
I am reading this book called Java Concurrency in Practice and the author gives
I'm trying to mastery php by reading book, and i'm stuck on this code
While reading the book Programming Ruby , one example shows how blocks can be

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.