Re: AND Operation on a two dimensional array
On Jan 15, 3:13 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
Mugunth wrote:
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
.
The result should be another array that contains only 6.
Any ideas?
You're not explaining exactly what it is your are trying to
accomplish, but it seems like a non optimal soulution would
be:
sort each row
store the first row in an array we'll call working
go through each row and remove from working any value that is
not in the row return whatever is left
Not sure how you can speak about a solution if you (admittedly)
don't know what the problem is:-). If it's what you think, then
sort each row, then use std::set_intersection. Something like:
typedef std::vector< int >
Row ;
typedef std::vector< Row >
Table ;
Row
findCommonElements(
Table::const_iterator
begin,
Table::const_iterator
end )
{
Row result ;
Table::const_iterator
current = begin ;
if ( current != end ) {
result = *current ;
std::sort( result.begin(), result.end() ) ;
++ current ;
}
while ( current != end ) {
Row tmp1( result ) ;
Row tmp2( *current ) ;
std::sort( tmp2.begin(), tmp2.end() ) ;
result.clear() ;
std::set_intersection(
tmp1.begin(), tmp1.end(),
tmp2.begin(), tmp2.end(),
std::back_inserter( result ) ) ;
++ current ;
}
return result ;
}
You are actually looking for a subset. The subset of the
elements that are in each set (row).
Is this the case?
Your description sounds more like a set intersection, treating
each row as a set.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34