C ++: subtract vectors

I have two vectors. And I need to remove from vector1 what is in vector2.

[UPDATE: it is unclear whether this means subtracting for each item according to the link below or to set the difference]

I am using Visual Studio 2010.

There seems to be a method: http://msdn.microsoft.com/en-us/library/system.windows.vector.subtract.aspx

But this somehow does not work and there is not even a code sample.

Could you help me? If the standard method does not exist, perhaps you could suggest how to organize it using loops? Thank you in advance.

#include "stdafx.h"; #include <vector>; #include <iostream> using namespace std; int main () { vector<int> vector1; vector<int> vector2; for (int i = 0; i < 10; i++) { vector1.push_back (i); } for (int i = 0; i < 6; i++) { vector2.push_back (i); } myvector1 = Subtract(vector1, vector2); return 0; } 
+8
source share
5 answers

You should use std::set_difference : http://en.cppreference.com/w/cpp/algorithm/set_difference

First you will need to sort your vectors , since set_difference works with sorted ranges. That is, if they are not already sorted (as in your use case).

 std::sort(vector1.begin(), vector1.end()); std::sort(vector2.begin(), vector2.end()); 

Then you call it like this:

 std::vector<int> difference; std::set_difference( vector1.begin(), vector1.end(), vector2.begin(), vector2.end(), std::back_inserter( difference ) ); 

This will add to the difference those elements that are in vector1 that are not found in vector2 .

+21
source

std::transform(vector1.begin(), vector1.end(), vector2.begin(), vector1.begin(), std::minus<int>())

The 4th argument is the place of the result. It should work even if the size of the vectors is different.

+7
source

If you do not want to use std::set_difference , you can do this:

 // substracts b<T> to a<T> template <typename T> void substract_vector(std::vector<T>& a, const std::vector<T>& b) { typename std::vector<T>::iterator it = a.begin(); typename std::vector<T>::const_iterator it2 = b.begin(); while (it != a.end()) { while (it2 != b.end() && it != a.end()) { if (*it == *it2) { it = a.erase(it); it2 = b.begin(); } else ++it2; } if (it != a.end()) ++it; it2 = b.begin(); } } 

This will remove from a all the values ​​that are in b .

Good luck

+1
source

I would suggest switching to EigenVectors, which has built-in arithmetic operations for vectors:

http://eigen.tuxfamily.org/dox-devel/group__TutorialMatrixArithmetic.html

Thus, you can use operators such as +, -, *, /

 #include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d a; a << 1, 2, 3, 4; MatrixXd b(2,2); b << 2, 3, 1, 4; std::cout << "a + b =\n" << a + b << std::endl; std::cout << "a - b =\n" << a - b << std::endl; std::cout << "Doing a += b;" << std::endl; a += b; std::cout << "Now a =\n" << a << std::endl; Vector3d v(1,2,3); Vector3d w(1,0,0); std::cout << "-v + w - v =\n" << -v + w - v << std::endl; } 
0
source

/The easiest way /

 #include<stdio.h> main() { int A,B,C; printf("enter the two numbers 1st 2nd="); scanf("%d",&A); scanf("%d",&B); C=AB; printf("Result="); printf } 
-3
source

All Articles