Is it possible to subtract a vector from a vector?

I do not know, I do not understand just how to implement this task.

Develop a complete program in which, using the appropriate constructors, create three vectors v1, v2, v3 with elements of an integer type, sizes 5, 7, 6, respectively, and the same values of elements, respectively 1, 2, 3. Display the dimensions of the vectors, the values of their elements, and perform the assignment v3=v2-v1. After that, display the dimensions of the vectors and their values again elements.

Maybe there instead of - should be =, or you can still implement subtraction?

// laba9.cpp: определяет точку входа для консольного приложения.
//

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    setlocale (LC_ALL, "russian");
    vector<int> v1(5,1), v2(7,2), v3(6,3);
    cout << "Число элементов в V1: " << v1.size() << endl;
    cout << "Элементы в V1: ";
    for(int i=0; i<v1.size(); i++)
        cout << v1[i] << " ";
    cout << endl << endl;
    cout << "Число элементов в V2: " << v2.size() << endl;
    cout << "Элементы в V2: ";
    for(int i=0; i<v2.size(); i++)
        cout << v2[i] << " ";
    cout << endl << endl;
    cout << "Число элементов в V3: " << v3.size() << endl;
    cout << "Элементы в V3: ";
    for(int i=0; i<v3.size(); i++)
        cout << v3[i] << " ";
    cout << endl << endl;

    system ("pause");
    return 0;
}

Please help me, give me an idea, thank you.

Author: Gomer Simpson, 2018-05-06

1 answers

We sit down and write our own operator, like

vector<int> operator-(const vector<int>&a, const vector<int>&b)
{
    if (a.size() != b.size())
        throw("a.size() != b.size()"); // Или как-то иначе обработать разные размеры
    vector<int> c(a.size());
    for(size_t i = 0; i < a.size(); ++i)
        c[i] = a[i] - b[i];
    return c;
}

Everything. It remains to apply :)

 2
Author: Harry, 2018-05-07 03:26:12