473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem with hash set iter

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
3 4022
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
3
by: John Harrison | last post by:
This might be a compiler problem or it might be some subtlety of the language I don't understand. I'm writing an iterator and I want to write a version of std::distance specially for my...
0
by: joenchinghkg | last post by:
I am going to create an method to insert Items inside the hash table, However, i don't really know what I should code inside the method. Should I use push or pop because it is based on a list...
15
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
0
by: Fei Liu | last post by:
Yet another problem to deal with dynamic data type that can only be determined at run time. For a netCDF file (a scientific data format), a variable is defined with its associating dimensions, i.e....
8
by: Christian Bruckhoff | last post by:
Hi. I got a problem with deleting items of a vector. I did it like this: void THIS::bashDelPerson() { cout << "Bitte Suchstring eingeben: "; char search; cin >search;...
8
by: Mike Jolley | last post by:
Hello First off, I'm a student so I'm pretty new to C++, and therefore I have probably made a stupid mistake somewhere. Anyway Ive been trying to fix this 5 hours straight now, so i need a...
8
by: Fab | last post by:
All, I need your help understanding why the following code does *NOT* compile with G++ (tested with gcc 3.x and 4.1.x): ---------------------------------------------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.