compare vectors for inequality
Jan 12, 2011 at 1:04pm UTC
Hello everyone
I have to compare two vectors (having struct data from two different files) for inequality. The two vectors contain struct type of elements. E-g
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Struct StructVect1
{
string word_1;
string word_2;
string word_3;
string word_4;
};
Struct StructVect2
{
string word_1;
string word_2;
string word_3;
string word_4;
};
and suppose vector1 has 10 (struct type) elements in it and vector2 has 5 (struct type) elements in it.
I want to find out all those elements of vector1 the word_1 (e-g vect1[i].word_1) of which is NOT equal to word_4 of any element of the vector2 (e-g vect2[1].word_4, vect2[2].word_4, vect2[3].word_4, vect2[4].word_4).
For example
1 2 3 4 5 6 7 8 9
if (
vect1[0].word_1 != vect2[0].word_4
vect1[0].word_1 != vect2[1].word_4
vect1[0].word_1 != vect2[2].word_4
vect1[0].word_1 != vect2[3].word_4
)
{
// push_back vect1[0] to vect3[0]
}
Could you please help.
Jan 12, 2011 at 2:00pm UTC
I'm not sure why you have two identical structures with differing names. Just name them the same thing:
1 2 3 4 5 6 7
struct FourWords
{
string word_1;
string word_2;
string word_3;
string word_4;
};
Next, you need a way to compare two structures. Overload the equality operator for that:
1 2 3 4 5 6 7
bool operator == ( const FourWords& lhs, const FourWords& rhs )
{
return (lhs.word_1 == rhs.word_1)
and (lhs.word_2 == rhs.word_2)
and (lhs.word_3 == rhs.word_3)
and (lhs.word_4 == rhs.word_4);
}
What you want to do is similar. You want to have a little function that will compare your two things properly:
1 2 3 4
bool words_1_and_4_not_equal( const FourWords& lhs, const FourWords& rhs )
{
return lhs.word_1 != rhs.word_4;
}
Once you have that, you only need a little function to do what you want:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void copy_mismatch(
const vector <FourWords> & src,
const vector <FourWords> & cmp,
vector <FourWords> & dest )
{
vector <FourWords> ::const_iterator srciter = src.begin();
vector <FourWords> ::const_iterator cmpiter = cmp.begin();
while (srciter != src.end())
{
if (words_1_and_4_not_equal( *srciter, *cmpiter ))
{
dest.push_back( *srciter );
}
}
}
copy_mismatch( vect1, vect2, vect3 );
You can generalize your code into a generic algorithm, if you like:
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
template <
typename InputIterator1,
typename InputIterator2,
typename OutputIterator,
typename Predicate
>
OutputIterator
copy_mismatch(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
OutputIterator result,
Predicate predicate )
{
while (first1 != last1)
{
if (predicate( *first1, *first2 ))
{
*result++ = *first1;
}
++first1;
++first2;
}
return result;
}
1 2 3 4 5 6 7 8 9
#include <iterator> // for that back_inserter <>
copy_mismatch(
vect1.begin(),
vect1.end(),
vect2.begin(),
back_inserter( vect3 ),
words_1_and_4_not_equal
);
(But you don't have to...)
Anyway, I hope this helps.
Jan 12, 2011 at 2:14pm UTC
Duoas! the structures are actually different. They have different number of variables.
variable "word_1" of struct "struct1" contains the following values
and variable "word_7" of struct "struct2" contains the following values
Many thanks. Let me try your solution
Topic archived. No new replies allowed.