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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:06:22+00:00 2026-05-20T06:06:22+00:00

I guess that there should be someway to write the below piece of code

  • 0

I guess that there should be someway to write the below piece of code without using “for” loops and by just using STL algorithms and iterators. If I am not wrong can anyone guide me on how to do this?

std::vector<double> A(N);
std::vector<double> B(N);
std::vector<double> C(N);
std::vector<double> D(N);

for(int i = 0; i < N; ++i)
  A[i] = myFunction1(i);

for(int i = 0; i < N; ++i)
  B[i] = myFunction2(A[i], i);

for(int i = 0; i < N; ++i)
  C[i] = myFunction3(A[i], B[i]);

for(int i = 0; i < N; ++i)
  D[i] = myFunction4(A[i], B[i], i);
  • 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-20T06:06:23+00:00Added an answer on May 20, 2026 at 6:06 am
    typedef boost::counting_iterator<int> counter;
    
    std::transform(counter(0), counter(N), A.begin(), myFunction1);
    std::transform(A.begin(), A.end(), counter(0), B.begin(), myFunction2);
    std::transform(A.begin(), A.end(), B.begin(), C.begin(), myFunction3);
    

    Now write your own version of std::transform that takes a ternary function:

    template <typename In1, typename In2, typename In3, typename Out, typename FUNC>
    Out transform3(In1 first1, In1 last1, In2 first2, In3 first3, Out out, FUNC f) {
        while (first1 != last1) {
            *out++ = f(*first1++, *first2++, *first3++);
        }
        return out;
    }
    
    transform3(A.begin(), A.end(), B.begin(), counter(0), D.begin(), myFunction4);
    

    I guess there might be something you can do with variadic templates in C++0x to get a transform_N, but if so I don’t know what it is, I’ve never used them. Not sure if you can forward a variable number of arguments with modifications (in this case wrapping * ++ around each one, as it were).

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

Sidebar

Related Questions

Are there any open source (or I guess commercial) packages that you can plug
My guess is that this javascript just finds the div called divid and then
I'm reading some C++ code and notice that there are "#include" both in the
There is similar question to this one here but I guess that by this
Guess that I have a TextView that I want to update it in some
I guess that the # Net column is the most interesting, although I don't
i guess that many of us has acceptable years of experience in PHP programming,
I guess that I am missing something here. After having found this sample within
Delphi XE2, so I guess that is Indy 10(?). One server, 10 clients. I
My guess is that <br> might be inline and that <hr> might be block.

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.