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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:42:40+00:00 2026-05-26T13:42:40+00:00

Possible Duplicate: Advantages of std::for_each over for loop So I was playing around with

  • 0

Possible Duplicate:
Advantages of std::for_each over for loop

So I was playing around with some C++11 features and I’m curious as to why std::for_each is beneficial. Wouldn’t it be easier and look cleaner to do a for loop or is it because I’m so used to doing it this way?

#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>

typedef std::tuple<int, int> pow_tuple;

pow_tuple pow(int x)
{
    return std::make_tuple(x, x*x);
}

void print_values(pow_tuple values)
{
    std::cout << std::get<0>(values) << "\t" << std::get<1>(values) << std::endl;
}

int main(int argc, char** argv)
{
    std::vector<int> numbers;
    for (int i=1; i < 10; i++)
        numbers.push_back(i);
    std::for_each(numbers.begin(), numbers.end(),
        [](int x) { print_values(pow(x)); }
        );

    std::cout << "Using auto keyword:" << std::endl;
    auto values = pow(20);
    print_values(values);
    return 0;
}
  • 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-26T13:42:41+00:00Added an answer on May 26, 2026 at 1:42 pm

    It depends somewhat on the local coding conventions, but there are two
    potential advantages. The first is that it states clearly that the code
    iterates over all of the elements in the sequence; unless the local
    coding conventions say otherwise (and they are enforced), you have to
    consider that some cowboy programmer might have inserted a break. The
    second is that it names the operation you are performing on each
    element; this once can easily be handled by calling a function in the
    loop, and of course, really trivial operations may not need a name.

    There’s also the advantage, at least if you aren’t yet using C++11, that
    you don’t have to spell out the iterator types; the spelled out iterator
    types create a lot of verbiage, in which the important logic can get
    lost or overlooked.

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

Sidebar

Related Questions

Possible Duplicate: Advantages of SQL Server 2008 over SQL Server 2005? What are the
Possible Duplicate: B- trees, B+ trees difference What are the advantages/disadvantages of BTree+ over
Possible Duplicate: C++ char* vs std::string Is there any advantage to using char*'s instead
Possible Duplicate: What's the point of OOP? What are the advantages of using object-orientated
Possible Duplicate: What Advantages of Extension Methods have you found? All right, first of
Possible Duplicate: What are the advantages of installing programs in AppData like Google Chrome?
Possible Duplicate: Advantages of using forward Could someone please explain to me what perfect
Possible Duplicate: List<T> vs BindingList<T> Advantages/DisAdvantages what is the difference between IList and IBindingList
Possible Duplicate: String, StringBuffer, and StringBuilder what is the difference (advantages, disadvantages) between using
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should

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.