Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to get the difference of two vectors by elements that are in the first vector only in C++

1 Answer

0 votes
#include <iostream> 
#include <algorithm>
#include <vector> 

using std::vector;
using std::cout;  
using std::endl;

int pairwise_matches(vector<int> v1, vector<int> &v2)
{
	int sum = 0;

	for (int i = 0; i < v1.size(); i++)
		if (v1[i] == v2[i])
			sum++;

	return sum;
}

int main()
{
	vector<char> v1{ 'a','b', 'c', 'd', 'h' }, v2{ 'a', 'c', 'e', 'f', 'g' }, v3(10);
	vector<char>::iterator p, v3_end;

	v3_end = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());

	for (p = v3.begin(); p != v3_end; p++)
		cout << *p << ' ';

	cout << endl;

	return 0;
}

/*
run:

b d h

*/

 



answered Apr 25, 2018 by avibootz
...