GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


hash_set funniness by baldurk
1
 
#include <iostream>
#include <ext/hash_map>
#include <ext/hash_set>
using namespace std;
using namespace __gnu_cxx;

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

typedef hash_set<const char *, hash<const char *>, eqstr> Dictionary;

void lookup(Dictionary& Set,
                    const char* word)
{                               
      Dictionary::const_iterator it
              = Set.find(word); 
        cout << word << ": "    
                   << (it != Set.end() ? "present" : "not present")
                          << endl;
}                               
                                
Dictionary Set;                 
                                
int main()                      
{                               
    FILE *f = fopen("words", "r");
    if(!f)                      
        return 1;               
                                
    while(!feof(f))             
    {                           
        char *line = 0;size_t n;
        int ret = getline(&line, &n, f);
        line[strlen(line)-1] = 0;
        if(line[0] == 0 || n == 0 || ret == -1)
        {                       
            free(line);         
            continue;           
        }                       
        cout << "inserting " << line << " in dict" << endl;
        Set.insert(line);       
        free(line);             
    }                           
                                
    fclose(f);                  
                                
    const char word[] = "lol";  
                                
    for(int i=0; i < 100; i++)  
    {                           
        Dictionary::const_iterator it = Set.find(word);
        if(it == Set.end())     
            cout << "WTF" << endl;
    }                           
}                               

// make the "words" file:
// lol
// foo
// bar