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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:47:25+00:00 2026-06-02T00:47:25+00:00

So, one of my classes has the following overload: DVector &operator+=(DVector const &other) {

  • 0

So, one of my classes has the following overload:

DVector &operator+=(DVector const &other) {
    if (vector.size() >= other.vector.size()) throw up; // lol.

    std::for_each(other.vector.begin(); other.vector.end(), [](DVector const &pass) {
        // huh?
    });
}

So, the idea is to sum every member of both vectors (uhm, every DVector instance contains a std::vector<float> member called vector), for example:

If I had one DVector containing a vector member containing the following floats: 11.0, 23.5, 12.3, and then another one containing 14.0, 6.5, 7.7, the sum of both should lead to the first vector holding 25.0, 25.0, 25.0.

The question: is there any way to loop through both vectors and sum their members using only one iterator, assuming that the size of the vectors isn’t a problem, or am I just forced to use a for (auto x: vector.size())?

Cheers, Julian.

  • 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-06-02T00:47:27+00:00Added an answer on June 2, 2026 at 12:47 am

    The is a std::transfom algorithm that take in two inputs and an output iterator.

    I don’t know how you specific vector design is consturcted but you should be able to modify the following for your needs.

     std::transform(input1.begin(), input1.end(),  //input1
                    intput2.begin(),               //input2
                    input1.begin(),                //output 
         [](double a, double b){
              return a+b;
         });
    

    You can also have a different output iterator for you operator + also well.

    Here are some references

    http://en.cppreference.com/w/cpp/algorithm/transform

    http://www.cplusplus.com/reference/algorithm/transform/

    And when you have a different output a back_inserter is your friend:

    http://en.cppreference.com/w/cpp/iterator/back_inserter

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

Sidebar

Related Questions

I have two classes one of which inherits from the other. Im trying to
I'm trying to simply use a vector within one of my classes. When trying
I have the following situation: Three concrete service classes implement a service interface: one
I have the following calls in one of my classes @Override public Integer save(NewsItem
I'm getting the following compile error in one of my classes, using gcc 3.4.5
One of my classes declares a templated function: template<class A, class B> A do_something(const
I'm executing the java binary from one of my classes, and a ClassNotFoundException gets
I have a field for addedBy in one of my classes. I've set up
Trying to override a tostring in one of my classes. return string.Format(@ name =
I have a Visual Studio C++ solution with 3 projects: one contains all classes

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.