Home » C++ STL

How to find common elements between two Vectors using in C++ STL?

C++ STL | finding common elements of two vectors: Here, we are going to learn how to find the common elements between two vectors using C++ STL program?
Submitted by IncludeHelp, on May 24, 2019

Given two vectors and we have to find their common elements.

Finding common elements between two vectors

To find common elements between two vectors, we can use set_intersection() function, it accepts the iterators of both vectors pointing to the starting and ending ranges and an iterator of result vector (in which we store the result) pointing to the starting position and returns an iterator pointing to the end of the constructed range.

Note: To use vector – include <vector> header, and to use set_intersection() function – include <algorithm> header or we can simply use <bits/stdc++.h> header file.

Syntax:

    std::set_intersection(
        iterator start1, iterator end1, 
        iterator start1, iterator end1, 
        iterator start3);

Here, iterator start1, iterator end1 – are the iterators pointing to the starting and ending position of first vector, iterator start2, iterator end2 – are the iterators pointing to the starting and ending position of second vector, and iterator start3 – is an iterator pointing to the starting position of the result vector.

C++ STL program to find the common elements of two vectors

//C++ STL program to find common elements
//between two Vectors
#include <bits/stdc++.h>
using namespace std;

int main()
{
    //vectors
    vector<int> v1 = { 10, 20, 5, 40, 2, 30 };
    vector<int> v2 = { 100, 10, 20, 30, 200, 300 };

    //sorting the vectors
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());

    // Print the vectors
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    cout << "v2: ";
    for (int x : v2)
        cout << x << " ";
    cout << endl;

    //declaring result vector to
    //store the common elements
    vector<int> v3(v1.size() + v2.size());

    //iterator to store return type
    vector<int>::iterator it, end;

    end = set_intersection(
        v1.begin(), v1.end(),
        v2.begin(), v2.end(),
        v3.begin());

    cout << "Common elements v3: ";
    for (it = v3.begin(); it != end; it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

v1: 2 5 10 20 30 40
v2: 10 20 30 100 200 300
Common elements v3: 10 20 30





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.