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.
/* 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(unsignedint i=0;i<mod.size();i++){
for(unsignedint 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;}elseif(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 */
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?
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.
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;
} elseif (mod[i] != pat[k]) {
found = mod[i];
}
}
if (found != 0) { // to trigger this and not save garbage
temp.push_back(found);
}
}
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.
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.
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/
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.