954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

find method not working for a list.

Im making a separate chaining hash table for dictionary words. In my insert method i use find to make sure there isnt a duplicate. Every thing compiles but my program stops at the find statement and quits working. If i omit the find method, which ok for the first word because there is no duplicate as of yet, it stops on the push back method for some weird reason. If someone could point out to me why it does this it would be greatly appreciated. I can post more of the code if needed.

Note:I also am not sure if my constructor is wrong or not.

//insert
bool DictHash::insert(const string &x)
{
    list<string> &newList = wordList[myHash(x)];     
    if(find(newList.begin(), newList.end(), x)!= newList.end())
    {        
        return false;
    }    
    newList.push_back(x);    
    if(++currentSize > wordList.size())
        rehash();
    return true;
}


//constructor
DictHash::DictHash(int size)
{
    vector<list<string> > wordList[size];
}

//main

using namespace std;

int main()
{
    ifstream infile("file name");
    DictHash *dictionary;
    dictionary = new DictHash(102001);
    if(infile.is_open())
    {
        while(infile.good())
        {
            string word;
            getline(infile, word);
            cout<<word<<endl;
            dictionary->insert(word);
        }
        infile.close();
    }
    return 0;
}
Howdydoody
Newbie Poster
24 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Your constructor declares a local array of size "size" called wordList. It will not initialize the data member of DictHash called wordList. I would assume that you use std::map for the type of wordList. In that case, it should work without initialization. Otherwise, you need to use an std::vector for wordList, and initialize it as follows:

//constructor
DictHash::DictHash(int size) : wordList(size) 
{
    
}


What is after the : is called the initialization list, and it is basically where you call all the constructors of all (or some of) the data members of the class, with whatever parameter they need.

mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

dude you are awesome. thanks so much.

Howdydoody
Newbie Poster
24 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: