C++ Hash_Set (GNU) - Bug?

Joined
Apr 8, 2008
Messages
1
Reaction score
0
While I know that hash_set is not a part of the standard C++ yet, but I need it for some application. I wrote the following test code for that which behaves weirdly.

It gives an intersection-size = 2 (should be 4). The intersecting elements apparently are 8112 33800. Clearly that is wrong.

BUT, if I replace the numbers such that all the 6 numbers are the same, then I get the intersection size to be 6 and all the 6 right intersections.

Also, for some other permutations and combinations of numbers, I get some right and some wrong. When I tried to intersect a hash_sett with 21 members with another having 97 members (with around 15 overlapping), I got a 0 overlap.

Can someone figure out what may be wrong with this?

[Note: uname -a gives:
Linux <my_machine_name> 2.4.21-40.ELsmp #1 SMP <current date/time> i686 i686 i386 GNU/Linux]

******************
#include <iostream>
#include <ext/hash_map>
#include <ext/hash_set>

using namespace std;
using namespace __gnu_cxx;

void retainAll(hash_set<int>& source, hash_set<int>& filter) {
hash_set<int> result;
insert_iterator<hash_set<int> > v_res(result, result.begin());
set_intersection(source.begin(), source.end() , filter.begin(), filter.end(), v_res);
source.clear();
source = result;
cout<<"Size of intersection: "<<result.size()<<" "<<source.size()<<endl;
}

int findIntersection(hash_set<int>& first, hash_set<int>& second) {
hash_set<int> result;
insert_iterator<hash_set<int> > v_res(result, result.begin());
set_intersection(first.begin(), first.end(), second.begin(), second.end(), v_res);
return result.size();
}
int main()
{
hash_set<int> hs1,hs2;

hs1.insert(8112);
hs1.insert(55031);
hs1.insert(33800);
hs1.insert(5049);
hs1.insert(42110);
hs1.insert(42111);

hs2.insert(8112);
hs2.insert(5031);
hs2.insert(33800);
hs2.insert(5049);
hs2.insert(42110);
hs2.insert(426111);

int interSize = findIntersection(hs1,hs2);
cout<<"Intersection size: "<<interSize<<endl;

retainAll(hs1,hs2);
hash_set<int>::iterator hsiter;
for (hsiter = hs1.begin(); hsiter != hs1.end(); hsiter++)
cout<<*hsiter<<" ";
cout<<endl;
return 0;
}
*****************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Crossword 14
Crossword 2
help needed in c++ 1
help needed c++ 5

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top