I am wondering how I could add the values of two vectors, like so:
vector<int> v1 = {3, 1, 7};
vector<int> v2 = {6, 5};
vector<int> v3 = {3, 8, 2}; // The sum of v1 and v2
The result here should be 382, as 317 + 65 yields 382.
Is there a way to do this? The sizes of the vectors are quite tricky. For example, the 2nd element in v1 is 1, but the 2nd element in v2 is 5. However, 5 must be added to the 7, not the 1.
edit: Forgot to mention that the vectors can be infinitely long. Casting to an int and then back to a vector could cause loss of numbers.
Here's a simple approach I devised on the conditions required: (considering vectors v1
and v2
, of data type DT
)
0
at the end by using std::vector<>::insert(iterator, 0)
with the iterator set to the beginning, since you'll need the zeroes to come first for proper element-to-element addition. Prior to that though, check which of the two vectors is larger in size, then collect the difference and insert for the same amount of times in a loop: int diff;
if(v1.size() > v2.size())
{ diff = v1.size() - v2.size();
for(int i = 0; i < diff; ++i)
v2.insert(v2.begin(), 0);
}
else
{ diff = v2.size() - v1.size();
for(int i = 0; i < diff; ++i)
v1.insert(v1.begin(), 0);
}
std::plus
from the functional
header for adding elements of one vector (say v1
) to the other vector (v2
) element-wise, emplaced with proper iterator positions in a std::transform
:std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::plus<DT>());
This will collect the element-wise sum of v1
and v2
into v1
(interchangeable). The only remaining issue or condition to deal with is the overflow for cases when element-wise additions sum up to be greater than or equal to 10.
1
to the next element in cases of overflow (>=10
) and assign the current vector element to its remainder when divided by 10. However, for the first element to overflow, we'll need to assign another element to the vector's beginning (eg: {3, 1} + {9, 2} = {1, 2, 3}) , which will be a 1
again (considering single digit vector elements), for which we can impose seperate if
statements: for(int i = v1.size(); i > 0; --i)
{
if(i == 1 && v1[1] >= 10)
{ v1[1] %= 10;
v1.insert(v1.begin(), 1);
}
else if(i != 1 && v1[i-1] >= 10)
{ v1[i - 1] %= 10;
v1[i - 2] += 1;
}
}
Examples:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#define DT long long int
int main()
{
std::vector<DT> v1 = {5, 2, 5, 7, 8};
std::vector<DT> v2 = {4, 5, 6};
// 52578
// 00456
// -----
// Expected output: // 53034
// Size management:
int diff;
if(v1.size() > v2.size())
{ diff = v1.size() - v2.size();
for(int i = 0; i < diff; ++i)
v2.insert(v2.begin(), 0);
}
else
{ diff = v2.size() - v1.size();
for(int i = 0; i < diff; ++i)
v1.insert(v1.begin(), 0);
}
// Element-wise addition:
std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(),std::plus<DT>());
// Overflow management:
for(int i = v1.size(); i > 0; --i)
{
if(i == 1 && v1[1] >= 10)
{ v1[1] %= 10;
v1.insert(v1.begin(), 1);
}
else if(i != 1 && v1[i - 1] >= 10)
{ v1[i - 1] %= 10;
v1[i - 2] += 1;
}
}
// Display the sum:
for(auto v:v1)
std::cout << v;
}
Output: 53034
.
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#define DT long long int
int main()
{
std::vector<DT> v1 = {5, 2, 5, 7};
std::vector<DT> v2 = {9, 3, 7, 2};
// 5257
// 9372
// -----
// Expected output: // 14629
// Size management:
int diff;
if(v1.size() > v2.size())
{ diff = v1.size() - v2.size();
for(int i = 0; i < diff; ++i)
v2.insert(v2.begin(), 0);
}
else
{ diff = v2.size() - v1.size();
for(int i = 0; i < diff; ++i)
v1.insert(v1.begin(), 0);
}
// Element-wise addition:
std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(),std::plus<DT>());
// Overflow management:
for(int i = v1.size(); i > 0; --i)
{
if(i == 1 && v1[1] >= 10)
{ v1[1] %= 10;
v1.insert(v1.begin(), 1);
}
else if(i != 1 && v1[i-1] >= 10)
{ v1[i - 1] %= 10;
v1[i - 2] += 1;
}
}
// Display the sum:
for(auto v:v1)
std::cout << v;
}
Output: 14629
.