labheshr
July 18th, 2008, 09:44 PM
Hi Guys:
I am trying to solve a larger problem a part of which is using 2 sets.
lets get down to the code:
set<long> LongSet;
LongSet A, B;
LongSet::iterator it1,it2;
set A is a superset of set B. I have to find numbers in set A which are not present in set B and call a function (lets say func1() )for them.
i tried 2 ways to do this:
1. Using idea that elements in set are sorted by < order.
for(it1=A.find(*B.begin( ) ) , it2=B.begin( ); it1!=A.end( ); ++it1) //As A is superset of B, find the beginning element of set B in set A, iterate from that to end of set A. Note: Set A and Set B have same end element values...
{
if(*it1==*it2) //if element in set A = element in set B , increment it2 -iterator for set B.
it2++;
else{ func1 (*it1); } //otherwise if there is an element missing in Set B , it2> it1 and we need to call the func1 for missing element in set B, to which it1 is pointing to in set A.
} // for...
2. Using find method defined for sets...
for(it1=A.find(*B.begin( ));it1 !=A.end( ); ++it1)
{
if(B.find(*it1) == B.end( ) //for element in Set A, search for it in Set B, if not present call func1 for it pointing to that element...
func1(it1);
}//for....
Problems:
method 1: it gives me missing values upto certain extent and then starts to give wrong missing values i.e values that are not missing are also reported by it.
with method 2: it gives me only some missing values, not all....
Both the sets will have large number of elements say between 100-900 values each. Also no duplicates are present - set does'nt allow them either!
Any help regarding this wud be greatly appreciated. If you think there is a syntax error- do mention it , ive tried my best to code it as accurately as possible -
Thanks in advace!
-Labhesh.
I am trying to solve a larger problem a part of which is using 2 sets.
lets get down to the code:
set<long> LongSet;
LongSet A, B;
LongSet::iterator it1,it2;
set A is a superset of set B. I have to find numbers in set A which are not present in set B and call a function (lets say func1() )for them.
i tried 2 ways to do this:
1. Using idea that elements in set are sorted by < order.
for(it1=A.find(*B.begin( ) ) , it2=B.begin( ); it1!=A.end( ); ++it1) //As A is superset of B, find the beginning element of set B in set A, iterate from that to end of set A. Note: Set A and Set B have same end element values...
{
if(*it1==*it2) //if element in set A = element in set B , increment it2 -iterator for set B.
it2++;
else{ func1 (*it1); } //otherwise if there is an element missing in Set B , it2> it1 and we need to call the func1 for missing element in set B, to which it1 is pointing to in set A.
} // for...
2. Using find method defined for sets...
for(it1=A.find(*B.begin( ));it1 !=A.end( ); ++it1)
{
if(B.find(*it1) == B.end( ) //for element in Set A, search for it in Set B, if not present call func1 for it pointing to that element...
func1(it1);
}//for....
Problems:
method 1: it gives me missing values upto certain extent and then starts to give wrong missing values i.e values that are not missing are also reported by it.
with method 2: it gives me only some missing values, not all....
Both the sets will have large number of elements say between 100-900 values each. Also no duplicates are present - set does'nt allow them either!
Any help regarding this wud be greatly appreciated. If you think there is a syntax error- do mention it , ive tried my best to code it as accurately as possible -
Thanks in advace!
-Labhesh.