#include#include using namespace std; int main() { vector vec1{1, 2, 3, 4, 5}; vector vec2{5, 4, 3, 2, 1}; // subtract the values of vec1 and vec2 vector result(vec1.size()); for (int i = 0; i < vec1.size(); ++i) { result[i] = vec1[i] - vec2[i]; } // output the result for (int i : result) { cout << i << " "; } cout << endl; return 0; }
-4 -2 0 2 4
#include#include using namespace std; int main() { vector vec{1, 2, 3, 4, 5}; // subtract 2 from each value in vec vector result(vec.size()); for (int i = 0; i < vec.size(); ++i) { result[i] = vec[i] - 2; } // output the result for (int i : result) { cout << i << " "; } cout << endl; return 0; }
-1 0 1 2 3In this example, we declare a vector `vec` and subtract the scalar value of 2 from each element using the Vector Minus function. The Vector Minus function is part of the standard library in C++. It is included in the header file `