How to find a common CHAR elements (case sensitive) between two vectors

As the title suggests, I'm looking for a way that would find any char elements that are common between two char vectors,

in this instance, we have our input as:

 
  vec1 = { z, Z, h, d, I, A, Z, A }


 
 vec2 = { Z, A, m, M, i, D, h }


which would output:

 
Z A h


(Order of the output and how its sorted doesn't particularly matter to me, time complexity, and legibility as well)

I believe the process does involve having to convert the char vectors into integers in order to sort its ASCII value, and possibly setting up an iterator that would go through each char element in both vectors, I can't be sure what steps to take next. I'm still currently searching for a solution as I'm typing this question, so please any verification and clarity could be of great help, thank you. Please don't hesitate to ask further clarity.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

template<typename C> C get_intersection( C &a, C &b )
{
   sort( a.begin(), a.end() );
   sort( b.begin(), b.end() );  
   C result;
   set_intersection( a.begin(), a.end(), b.begin(), b.end(), back_inserter( result ) );
   return result;
}

template<typename C> void print( C c )
{
   for ( auto e : c ) cout << e << ' ';
   cout << '\n';
}


int main()
{
   vector<int> v1 = { 1, 45, 54, 71, 76, 12 };
   vector<int> v2 = { 1, 7, 5, 4, 6, 12 };
   print( get_intersection( v1, v2 ) );
    
   vector<char> c1 = { 'z', 'Z', 'h', 'd', 'I', 'A', 'Z', 'A' };
   vector<char> c2 = { 'Z', 'A', 'm', 'M', 'i', 'D', 'h' };
   print( get_intersection( c1, c2 ) );
   
   string s1 = "zZhdIAZA";
   string s2 = "ZAmMiDh";
   print( get_intersection( s1, s2 ) );
}


1 12 
A Z h 
A Z h 
Last edited on
lastchance's solution, while elegant, relies on the standard library to do the work for you.
If this is a homework assignment, that's probably not going to fly.

I believe the process does involve having to convert the char vectors
into integers in order to sort its ASCII value,

That's not necessary. chars are values.

Consider this. It avoids sorts and requires only one pass over each array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;

//  Mark bits for values present in the first vector
void mark(vector<char>& c1, bitset<256>& bs)
{
    for (int i = 0; i < c1.size(); i++)   //  256 possible ASCII values
        bs[c1[i]] = true;
}

//  Print any intersecting values (bit is set)
void print(const vector<char>& c2, const bitset<256>& bs)
{
    for (int i = 0; i < c2.size(); i++)
    {
        if (bs[c2[i]])
            cout << c2[i] << endl;
    }
}

int main()
{   
    vector<char> c1 = { 'z', 'Z', 'h', 'd', 'I', 'A', 'Z', 'A' };
    vector<char> c2 = { 'Z', 'A', 'm', 'M', 'i', 'D', 'h' };
    bitset<256> bs;

    mark(c1, bs);
    print(c2, bs);
}

Thanks you 2
Topic archived. No new replies allowed.