473,545 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(cons t 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("bb x.txt", ios::in);
if (infile.is_open ()) {
while (getline(infile , line)) {
pair<hash_set<c onst char*, hash<const char*>,
eqstr>::iterato r, 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_i terator 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 4032
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()(cons t 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("bb x.txt", ios::in);
if (infile.is_open ()) {
while (getline(infile , line)) {
pair<hash_set<c onst char*, hash<const char*>,
eqstr>::iterato r, 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_i terator 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
14431
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
1703
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 iterator. Here's some code (highly reduced of course). The issue is how the return type of std::distance should be written. #include <iostream> #include...
0
1707
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 container for my private attribute. Here is my code. hashtable.h Code:
15
3811
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 to do this? Here is a concrete example: If I have a string with sequences of CRLF and possibly just CR's, is there a simple way of replacing the...
2
4432
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 */ #include <time.h>
0
1521
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. data(time, z, x). Each dimension is defined as wel in the netCDF file, time(time), z(z), x(x), for example netcdf andrew_test_data {...
8
1943
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; vector<Person>::iterator iter; iter = persons.begin();
8
1938
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 little assistance. What I'm trying to do I am using inheritance to make some bookings for a marina, which are: Booking
8
1873
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): --------------------------------------------------------------------- template<class T> class myvector { // Just a simplification of std::vector class iterator { T a; iterator &operator++(); };
0
7484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.