common numbers

Oct 2, 2015 at 6:54pm
Hi! I have to create this code that is supposed to find the non common elements in two given vectors (of different lengths). Does anybody have an idea on how to do it? I was thinking about boolean variables but I'm not ssure about it :(
Thanks!
Oct 2, 2015 at 7:09pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <iterator>

//...

//If you are not allowed to change original vectors:
std::vector<int> non_common(std::vector<int> lhs, std::vector<int> rhs)
{
    std::sort(lhs.begin(), lhs.end());
    std::sort(rhs.begin(), rhs.end());
    std::vector<int> result;

    //http://en.cppreference.com/w/cpp/algorithm/set_symmetric_difference
    std::set_symmetric_difference(lhs.begin(), lhs.end(), 
                                  rhs.begin(), rhs.end(),
                                  std::back_inserter(result)); //http://en.cppreference.com/w/cpp/iterator/back_inserter
    return result;
}
Last edited on Oct 2, 2015 at 7:10pm
Topic archived. No new replies allowed.