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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:12:39+00:00 2026-05-14T19:12:39+00:00

I know I can do this y[i] += f(x[i]) using transform with two input

  • 0

I know I can do this y[i] += f(x[i]) using transform with two input iterators.
however it seems somewhat counterintuitive and more complicated than for loop.

Is there a more natural way to do so using existing algorithm in boost or Stl. I could not find clean equivalent.

here is transform (y = y + a*x):

using boost::lambda;
transform(y.begin(), y.end(), x.begin(), y.begin(), (_1 + scale*_2);
//  I thought something may exist:
transform2(x.begin(), x.end(), y.begin(), (_2 + scale*_1);
// it does not, so no biggie. I will write wrapper

Thanks

  • 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-14T19:12:40+00:00Added an answer on May 14, 2026 at 7:12 pm

    There are several ways to do this.

    As you noted you can use transform with a number of predicates, some more or less automatically generated:

    std::vector<X> x = /**/;
    std::vector<Y> y = /**/;
    
    assert(x.size() == y.size());
    
    //
    // STL-way
    //
    struct Predicate: std::binary_function<X,Y,Y>
    {
      Y operator()(X lhs, Y rhs) const { return rhs + f(lhs); }
    };
    
    std::transform(x.begin(), x.end(), y.begin(), y.begin(), Predicate());
    
    //
    // C++0x way
    //
    std::transform(x.begin(), x.end(), y.begin(), y.begin(),
                   [](X lhs, Y rhs) { return rhs += f(lhs); });
    

    Now, if we had a vector with the range of indices, we could do it in a more “pythony” way:

    std::vector<size_t> indices = /**/;
    
    
    //
    // STL-way
    //
    class Predicate: public std::unary_function<size_t, void>
    {
    public:
      Predicate(const std::vector<X>& x, std::vector<Y>& y): mX(x), mY(y) {}
      void operator()(size_t i) const { y.at(i) += f(x.at(i)); }
    private:
      const std::vector<X>& mX;
      std::vector<Y>& mY;
    };
    
    std::foreach(indices.begin(), indices.end(), Predicate(x,y));
    
    //
    // C++0x way
    //
    std::foreach(indices.begin(), indices.end(), [&](size_t i) { y.at(i) += f(x.at(i)); });
    
    //
    // Boost way
    //
    BOOST_FOREACH(size_t i, indices) y.at(i) += f(x.at(i));
    

    I don’t know if there could be something to do with views, they normally allow some pretty syntax. Of course it’s a bit difficult here I think because of the self-modifying y.

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 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've been struggling with this for two days right now. At the moment I
Using VB.NET on this one, in ASP.NET Web Forms. I have a property that
I've been trying to do TCP communication using my Wavecom Fastrack modem. What I
I'm reposting this question as I didn't get much of a response last time,
I need to convert a C/C++ double to a 64 bit two's complement, where
I am using a 3D engine called Electro which is programmed using Lua. It's
I'm new to Clojure and have been using Compojure to write a basic web
I would like to connect to a DB2 database, specifically an iSeries version, using
I'm trying to skip an INSERT in a particular case, where one of two
I'm developing this usercontrol which requires I override the WndProc of the control's parent

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.