Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy might stable_sort be affecting my hashtable values?
    primarykey
    data
    text
    <p>I have defined a struct ABC to contain an int ID, string NAME, string LAST_NAME;<br> My procedure is this: Read in a line from an input file. Parse each line into first name and last name and insert into the ABC struct. Also, the struct's ID is given by the number of the input line. </p> <p>Then, push_back the struct into a vector masterlist. I am also hashing these to a hashtable defined as vector&lt; vector >, using first name and last name as keywords. That is,</p> <p>If my data is:<br> Garfield Cat<br> Snoopy Dog<br> Cat Man </p> <p>Then for the keyword cash hashes to a vector containing Garfield Cat and Cat Man. I insert the structs into the hashtable using push_back again.</p> <p>The problem is, when I call stable_sort on my masterlist, my hashtable is affected for some reason. I thought it might be happening since the songs are being ordered differently, so I tried making a copy of the masterlist and sorting it and it still affects the hashtable though the original masterlist is unaffected.</p> <p>Any ideas why this might be happening? </p> <p>EDIT--source code posted:</p> <p>Here is the main</p> <pre><code> ifstream infile; infile.open(argv[1]); string line; vector&lt;file&gt; masterlist; vector&lt; vector&lt;node&gt; &gt; keywords(512); hashtable uniquekeywords(keywords,512); int id=0; while (getline(infile,line)){ file entry; if (!line.empty() &amp;&amp; line.find_first_not_of(" \t\r\n")!=line.npos){ id++; string first=beforebar(line,0); string last=afterbar(line); entry.first=first; entry.last=last; entry.id=id; masterlist.push_back(entry); int pos=line.find_first_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"); while (pos!=(int)line.npos){ string keyword=getword(line,pos); node bucket(keyword,id); bucket.addentry(entry); uniquekeywords.insert(bucket); } } } </code></pre> <p>Here is the snippet of hashtable insert implementation:</p> <pre><code>struct node{ string keyword; vector&lt;file&gt; entries; int origin; void addentry(file entry); node(string keyword, int origin); }; void hashtable::insert(node bucket){ int key=hashfunction(bucket.keyword); if (table[key].empty()){ table[key].push_back(bucket); numelt++; } else{ vector&lt;node&gt;::iterator it; it=table[key].begin(); while(it!=table[key].end()){ if (compare((*it).keyword,bucket.keyword)==0 &amp;&amp; (*it).origin!=bucket.origin){ (*it).entries.insert((*it).entries.end(),bucket.entries.begin(),bucket.entries.end()); (*it).origin=bucket.origin; return; } it++; } node bucketcopy(bucket.keyword,bucket.origin); table[key].push_back(bucket); numelt++; return; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload