difference between two vectors in c++

Apr 15, 2010 at 11:46am
Hello. I'd like to ask you guys a question about vectors: I'm trying to extract the difference between 2 vectors, but so far I've been unable to do it. I'm posting my code so you can read it and see where I'm wrong.

It only seems to work correctly for the first iteration, then it just prints the model (see below).

I'd appreciate it if you could post a correction or explain what's wrong with it (don't worry about syntax errors, just the logic in C++). Thank you.

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
/* Pre: vector model, vector pattern */
vector<int> vector_diff(vector<int> model, vector<int> pattern){
    vector<int> mod=model;
    vector<int> pat=pattern;
    vector<int> temp;
    int found=0;

    for(unsigned int i=0;i<mod.size();i++){
        for(unsigned int k=0;k<pat.size();k++){
            /* INTENT: if current item in mod is equal to current item in pat, skip
               the iteration of pat members and try the next in mod. if different,
               push it into vector */
            if(mod[i]==pat[k]){break;}else if(mod[i]!=pat[k]){
                found=mod[i]; // I think something is missing here
                continue;
            }
        /* INTENT: if item in mod was not equal to any other item in pat, push it into temp and continue */
        }if(found!=0){ // dont push '0' for the first iteration
            temp.push_back(found);
            continue;
        }
    }
    return temp;
}
/* Post: vector n contains elements model had that were not in pattern */
Last edited on Apr 15, 2010 at 12:30pm
Apr 15, 2010 at 12:15pm
If you want only logic errors, then you have to specify your problem more precisely.

What if model is the vector { 3, 3, 4 } and pattern is { 3 }? What should the result be?
What if model is the vector { 3, 4, 5 } and pattern is { 3, 3 }? What should the result be?

Is there a particular ordering required for the result? For example, if model is the vector
{ 5, 4, 3 } and pattern is { 4, 3 }, must the result be { 4, 3 }, { 3, 4 }, or does it not matter?
Apr 15, 2010 at 12:28pm
Hi, thank you for answering.

Model is 2,3,4,5,6,7,8,9,10
Pattern is 2,4,6,8,10

Result should be 3,5,7,9 (items in model that are not in pattern) but when I get to 4, it just prints model: 3,4,5,6,7,8,9,10
I've updated the code with new fixes, but it still doesn't work.

The ordering should be min to max (sorted), but I think I got that covered as it is.
Last edited on Apr 15, 2010 at 12:34pm
Apr 15, 2010 at 1:46pm
Above function is realized in somewhat painful way. Anyway, it might work if you zero found just before leave inner loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
  for (int i = 0; i < mod.size(); i++) {
    for (int k = 0; k < pat.size(); k++) {
      if (mod[i] == pat[k]) {
        found = 0; // add this
        break;
      } else if (mod[i] != pat[k]) {
        found = mod[i];
      }
    }
    if (found != 0) { // to trigger this and not save garbage
      temp.push_back(found);
    }
  }


continue are not needed here: http://www.cplusplus.com/doc/tutorial/control/
Apr 15, 2010 at 3:58pm
In that case, I would first convert the vectors to sets. This will remove the duplicates and sort them.
Then, I'd use the set_difference algorithm to return the items that are present in one set but not
the other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Warning: code not compiled!
#include <algorithm>
#include <iterator>
#include <set>
#include <vector>

std::vector<int> vector_diff( const std::vector<int>& model, const std::vector<int>& pattern )
{
    std::set<int> s_model( model.begin(), model.end() );
    std::set<int> s_pattern( pattern.begin(), pattern.end() );
    std::vector<int> result;

    std::set_difference( s_model.begin(), s_model.end(), s_pattern.begin(), s_pattern.end(),
        std::back_inserter( result ) );

    return result;
}

Apr 15, 2010 at 6:06pm
serge, I'll try your solution.

jsmith, you're probably right. But since the purpose of this code is to practice with vectors, I'll stick with it for a while, see if I can make it work.

Anyway, thank you both for taking the time to answer.
Apr 15, 2010 at 10:11pm
Either way I wouldn't recommend writing your own loop. If you are just practicing with vectors you can simply sort them with std::sort and then use the set_difference algorithm as jsmith uses. I see no reason why that algorithm won't work for the sorted vector.
http://cplusplus.com/reference/algorithm/set_difference/
Apr 16, 2010 at 7:35am
serge, It works, thank you very much for your help.

kempofighter, you are right, I just didn't know that algorithm existed.
I'm still learning and sometimes I make things difficult for myself inadvertently.

Anyway, see you around.
Topic archived. No new replies allowed.