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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:11+00:00 2026-05-26T20:05:11+00:00

Iterating over a vector works: std::vector<int> collection = {2, 3, 4, 5435345, 2}; std::for_each(collection.begin(),

  • 0

Iterating over a vector works:

std::vector<int> collection = {2, 3, 4, 5435345, 2};
std::for_each(collection.begin(), collection.end(), [](int& i){cout << i << endl;});

but not over a set (compile error):

std::set<int> collection = {2, 3, 4, 5435345, 2};
std::for_each(collection.begin(), collection.end(), [](int& i){cout << i << endl;});

Why can’t I iterate over a std::set with std::for_each?

Bonus question:
Also, I would like to change the int& in the lambda’s argument to auto&, why can’t this be automatically deduced?

  • 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-26T20:05:12+00:00Added an answer on May 26, 2026 at 8:05 pm

    std::set<T>::value_type is T const, not T; consequently, the argument to your lambda must be a value type (i.e., copy) or int const& (and technically, or int const volatile&), not int&. I.e., this works:

    std::set<int> collection{2, 3, 4, 5435345, 2};
    std::for_each(
        collection.begin(),
        collection.end(),
        [](int const& i) { std::cout << i << std::endl; }
    );
    

    Bonus question: Also, I would like to change the int& in the lambda’s argument to auto&, why can’t this be automatically deduced?

    Because the standard says it can’t; historically, I believe this was due to overly-complicated interactions between lambdas and concepts (before concepts were removed from the draft). However, I hear rumors that the first defect reports to the new (C++11) standard will address exactly this, so it’s possible that you’ll see support for this added to your compiler of choice within the next year or two. EDIT: Oh, look, C++14 now has polymorphic lambdas…

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

Sidebar

Related Questions

When iterating over std::map<X,std::vector<Y> > , may I sort the vectors, or might that
What is the correct way of iterating over a vector in C++? Consider these
When iterating over elements of a vector it is preferred to use iterators instead
I have a class (foo) that contains a vector. If i try iterating over
I'm iterating over a JRE Collection which enforces the fail-fast iterator concept, and thus
I'm iterating over records and want to create the toggle-options for records but I
I'm iterating over a vector, for each element I look something up in a
Short Description: I am iterating over a vector calling a virtual function on every
I am iterating over an array in MooTools but seeing additional items when iterating
Is it possible to add elements to a collection while iterating over it? More

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.