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

The Archive Base Latest Questions

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

The question is quite dumb, but I need to do it in a very

  • 0

The question is quite dumb, but I need to do it in a very efficient way – it will be performed over an over again in my code. I have a function that returns a vector, and I have to add the returned values to another vector, element by element. Quite simple:

vector<double> result;
vector<double> result_temp
for(int i=0; i< 10; i++) result_temp.push_back(i);

result += result_temp //I would like to do something like that.
for(int i =0; i< result_temp.size();i++)result[i] += result_temp[i]; //this give me segfault

The mathematical operation that I’m trying to do is

u[i] = u[i] + v[i] for all i

What can be done?

Thanks

EDIT: added a simple initialization, as that is not the point. How should result be initialized?

  • 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-16T01:07:20+00:00Added an answer on May 16, 2026 at 1:07 am

    If you are trying to append one vector to another, you can use something like the following. These are from one of my utilities libraries–two operator+= overloads for std::vector: one appends a single element to the vector, the other appends an entire vector:

    template <typename T>
    std::vector<T>& operator+=(std::vector<T>& a, const std::vector<T>& b)
    {
        a.insert(a.end(), b.begin(), b.end());
        return a;
    }
    
    template <typename T>
    std::vector<T>& operator+=(std::vector<T>& aVector, const T& aObject)
    {
        aVector.push_back(aObject);
        return aVector;
    }
    

    If you are trying to perform a summation (that is, create a new vector containing the sums of the elements of two other vectors), you can use something like the following:

    #include <algorithm>
    #include <functional>
    
    template <typename T>
    std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b)
    {
        assert(a.size() == b.size());
    
        std::vector<T> result;
        result.reserve(a.size());
    
        std::transform(a.begin(), a.end(), b.begin(), 
                       std::back_inserter(result), std::plus<T>());
        return result;
    }
    

    You could similarly implement an operator+= overload.

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

Sidebar

Related Questions

This is a very basic question...quite embarassing, but here goes: I have a Stopwatch
This might sound very dumb question but I am just confused. I am quite
A quite dumb question but i simply can't find the answer. I have a
Sorry if it is a dumb question, but the issue is quite straight forward.
The question is quite similar to this one , but I need to concentrate
I beg a pardon for quite a dumb question on url rewriting. I have
Many will find this question quite trivial, but since I'm quite new on iOS
I have seen the reverse of this question quite a few times, but have
I'm sorry if this will sound like a dumb question but I just can't
This question is probably quite dumb, but I can't find an example and can't

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.