By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

problem with hash set iter

P: n/a
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

Apr 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ra*******@gmail.com wrote:
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.
Apr 15 '06 #2

P: n/a
red floyd wrote:
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

Apr 16 '06 #3

P: n/a
ra*******@gmail.com wrote:
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
Apr 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.