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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:54:19+00:00 2026-05-16T20:54:19+00:00

I have the following code (compiler: MSVC++ 10): std::vector<float> data; data.push_back(1.0f); data.push_back(1.0f); data.push_back(2.0f); //

  • 0

I have the following code (compiler: MSVC++ 10):

std::vector<float> data;
data.push_back(1.0f);
data.push_back(1.0f);
data.push_back(2.0f);

// lambda expression
std::for_each(data.begin(), data.end(), [](int value) {
     // Can I get here index of the value too?
});

What I want in the above code snippet is to get the index of the value in the data vector inside the lambda expression. It seems for_each only accepts a single parameter function. Is there any alternative to this using for_each and lambda?

  • 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-16T20:54:19+00:00Added an answer on May 16, 2026 at 8:54 pm

    I don’t think you can capture the index, but you can use an outer variable to do the indexing, capturing it into the lambda:

    int j = 0;
    std::for_each(data.begin(), data.end(), [&j](float const& value) {
                j++;
    });
    std::cout << j << std::endl;
    

    This prints 3, as expected, and j holds the value of the index.

    If you want the actual iterator, you maybe can do it similarly:

    std::vector<float>::const_iterator it = data.begin();
    std::for_each(data.begin(), data.end(), [&it](float const& value) {
                // here "it" has the iterator
                ++it; 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • 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
  • Editorial Team
    Editorial Team added an answer I had the name of the key all along in… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer There are several use cases for a set. You could… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer Is there a best-practice in situations where one wants a… May 17, 2026 at 2:29 am

Trending Tags

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

Top Members

Related Questions

I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
The following source file will not compile with the MSVC compiler (v15.00.30729.01): /* stest.c
I have the following code and it fails to compile template < typename T
I have the following code: struct A { short b; }; struct B {
I have the following declaration in my code: u32 volatile __attribute__((nocast)) *A, *B; Is
I have the following assembly code (written for NASM on Linux): ; This code
The following code does not generate a compiler warning in D6. Can I make
I have the following piece of code: class ICookable { public: virtual void CookMe
Following code fails with a error message : t.cpp: In function `void test()': t.cpp:35:
I've run into a strange problem. The following simplified code reproduces the problem in

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.