Adding two vectors together

2019-07-26 10:28发布

问题:

So I have this problem I ran into when building a code. This the question

This work is based on operator overloading, you need to build a string calculator, the calculator can do add and minus functions for string variables (there will be only
characters and space for the string).

The problem I ran into is when I try to add the two vectors I created together. For example, Vector A= <1,2,3> and Vector B= <1,2>. I want A+B to equal <2,4,3>. But when I do that I get an output of 2. Here is my code.

#include<iostream>
#include<string>
#include<vector>
using namespace std;
string a;
string b;
int k, j, ab, x;
vector <int> scab;
int main() {

cout << "Input A: ";
getline(cin, a);
cout << "Input B: ";
getline(cin, b);
    vector<int> sca;
    vector<int> scb;
    // For A
    for (int i = 0; i < a.size(); i++) {
        sca.push_back(static_cast <int> (a[i]));
    }
    cout << "Input A: ";
    for (int j = 0; j < sca.size(); ++j)
    {
        cout << sca[j] << "\t";
    }
    cout << endl;
    cout << endl;
    // For B
    for (int p = 0; p < b.size(); p++) {
        scb.push_back(static_cast <int> (b[p]));
    }
    cout << "Input B: ";
    for (int j = 0; j < scb.size(); ++j)
    {
        cout << scb[j] << "\t";
    }

    scab.push_back(sca[j] + scb[j]);

    cout << endl;
    cout << endl;

cout << "A+B: " << scab[j] << "\t";

    system("pause");

}

Thank You in advanced.

回答1:

Try to use more from the standard library to make it easier:

auto size = std::max(sca.size(), scb.size());
sca.resize(size);
scb.resize(size);

auto scab = std::vector<int>(size);
std::transform(sca.begin(), sca.end(), scb.begin(), scab.begin(), std::plus<int>());


回答2:

It follows from your code that the vectors contain characters that represent digits. Also you should consider an overflow that can occur.

Here is a demonstrative program that shows how such an operator cna be overloaded for such vectors.

#include <iostream>
#include <algorithm>
#include <vector>

std::vector<int> operator +( const std::vector<int> &a, const std::vector<int> &b )
{
    auto less = []( const std::vector<int> &a, const std::vector<int> &b )
    {
        return a.size() < b.size();
    };

    std::vector<int> c( std::min( a, b, less ) );
    c.resize( std::max( a.size(), b.size() ), '0' );

    int overflow = 0;

    auto plus = [&overflow]( int x, int y )
    {
        int sum = x - '0' + y - '0' + overflow;
        overflow = sum / 10;
        return sum % 10 + '0';
    };

    std::transform( c.begin(), c.end(), std::max( a, b, less ).begin(), c.begin(), plus );

    if ( overflow ) c.push_back( overflow + '0' );

    return c;
}    

int main()
{
    std::vector<int> a( { '1', '2', '3' } );
    std::vector<int> b( { '1', '9' } );

    for ( int x : a ) std::cout << static_cast<char>( x ) << ' ';
    std::cout << std::endl;

    std::cout << "+" << std::endl;

    for ( int x : b ) std::cout << static_cast<char>( x ) << ' ';
    std::cout << std::endl;

    std::cout << "=" << std::endl;

    std::vector<int> c;
    c = a + b;

    for ( int x : c ) std::cout << static_cast<char>( x ) << ' ';
    std::cout << std::endl;
}

The program output is

1 2 3 
+
1 9 
=
2 1 4 

The code will be simpler if the vectors will contain integer values instead of characters.

Or the vectors could be declared as having the type std::vector<char>



标签: c++ vector