problem with hash set iter

R

railrulez

Hi,

Attached is a program which uses a hash_set, but I cant seem to get
find() or iterators working on it. I'm not sure whether hash_set is std
C++, but I dont know where else to ask.

-----------------------------

#include <iostream>
#include <fstream>
#include <ext/hash_set>
using namespace std;

struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

int main (int argc, char *argv[])
{
using namespace __gnu_cxx;
hash_set<const char*, hash<const char*>, eqstr> ips;
string line;

ifstream infile;
infile.open("bbx.txt", ios::in);
if (infile.is_open()) {
while (getline(infile, line)) {
pair<hash_set<const char*, hash<const char*>,
eqstr>::iterator, bool> p = ips.insert(line.c_str());
// if (!p.second)
// cout << "new " << *p.first << "\n";
// else
// cout << "exists " << *p.first <<
"\n";
}
infile.close();
}

// if (ips.find("88.8.82.113") != ips.end())
// cout << "yea\n";

for (hash_set<const char*, hash<const char*>,
eqstr>::const_iterator it = ips.begin();
it != ips.end(); it++) {
cout << *it << " ";
}
return 0;
}

---------------

The two commented out lines (ip.find("88.8.82.113")) is supposed to
exist in the hash as the file i read from has that ip (the input file
contains a few IP addresses, one per line). But the find () returns
ips.end() and the for loop is infinite. Tested on freebsd 5.4 with gcc
3.4.2

Thanks in advance for any help.

marq
 
R

red floyd

Hi,

Attached is a program which uses a hash_set, but I cant seem to get
find() or iterators working on it. I'm not sure whether hash_set is std
C++, but I dont know where else to ask.


hash_set is not part of the Standard. You might try the SGI site, which
should have details on it.
 
R

railrulez

red said:
hash_set is not part of the Standard. You might try the SGI site, which
should have details on it.

Yes, I actually perused the online documentation they have available
there before posting here. I believe I have understood the container
definition and member functions, and when I write a smaller program
with manual inserts (ip.insert('10.0.0.0') etc.) with the exact type
declaration as above, it works. I'm wondering if it has something to do
with the file reading loop.

I now realize it's not part of the standard, but I suppose many posters
here use non-standard libraries for their work, so I'd really
appreciate any pointers. Even some suggestions about stuff in my code
that could interfere with the hash insert would be great.

thanks,
marq
 
L

Larry I Smith

Hi,

Attached is a program which uses a hash_set, but I cant seem to get
find() or iterators working on it. I'm not sure whether hash_set is std
C++, but I dont know where else to ask.

-----------------------------

#include <iostream>
#include <fstream>
#include <ext/hash_set>
using namespace std;

struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

int main (int argc, char *argv[])
{
using namespace __gnu_cxx;
hash_set<const char*, hash<const char*>, eqstr> ips;
string line;

ifstream infile;
infile.open("bbx.txt", ios::in);
if (infile.is_open()) {
while (getline(infile, line)) {
pair<hash_set<const char*, hash<const char*>,
eqstr>::iterator, bool> p = ips.insert(line.c_str());

The above ips.insert() inserts a COPY of the pointer to a
TEMPORARY char buffer returned by line.c_str(); the content
of that temporary char buffer is not gauranteed to be valid
after 'line' is changed.
Each time thru the loop, the contents of 'line' is replaced
by the data just read. If hash_set is like STL containers,
it makes a copy of the input params and stores that copy;
so why not forget the 'char *' returned by line.c_str()
and just use 'line' (i.e. store a copy of the actual string
rather than a pointer the the temporary C-string returned
by c_str())?

Read up on string.c_str() to see the warnings about the
life span of the buffer pointed to by the reaturned
'char *'.
// if (!p.second)
// cout << "new " << *p.first << "\n";
// else
// cout << "exists " << *p.first <<
"\n";
}
infile.close();
}

// if (ips.find("88.8.82.113") != ips.end())
// cout << "yea\n";

for (hash_set<const char*, hash<const char*>,
eqstr>::const_iterator it = ips.begin();
it != ips.end(); it++) {
cout << *it << " ";
}
return 0;
}

---------------

The two commented out lines (ip.find("88.8.82.113")) is supposed to
exist in the hash as the file i read from has that ip (the input file
contains a few IP addresses, one per line). But the find () returns
ips.end() and the for loop is infinite. Tested on freebsd 5.4 with gcc
3.4.2

Thanks in advance for any help.

marq

Regards,
Larry
 

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


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top