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
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