help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problems with stl map iterators


From: Larry I Smith
Subject: Re: problems with stl map iterators
Date: Tue, 19 Apr 2005 16:10:41 GMT
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.7) Gecko/20050414

Christian Christmann wrote:
> Hi, 
> 
> I've some problems with iterators on hashtbales.
> 
> My hashtable.h 
> 
> class HashTable 
> { 
>     map<int, void*> nhash; 
>     map<int, void*> getNhash(); 
> ... 
> } 
> ---------- 
> 
> My hashtable.cpp: 
> 

getNHash() returns a COPY of 'nhash'.
It should probably return a reference to 'nhash',
e.g.:  'map < int, void * > &'

> map<int, void*> HashTable::getNhash() 
> { 
>   return nhash; 
> } 
> 
> void HashTable::Insert(int ky,void* entr) 
> { 
>   nhash.insert(pair<int, void*>(ky, entr)); 
> } 
> ... 
> 
> ---------- 
> 
> In another class I use this hashtable: 
> 
> HashTable lNodes; 

The next line gets the begin() of the COPY of 'nhash' returned by
getNHash()
> const std::map<int, void*>::const_iterator begin = lNodes.getNhash().begin(); 

The next line gets the end() of ANOTHER COPY of 'nhash' returned
bt getNHash().
> const std::map<int, void*>::const_iterator end = lNodes.getNhash().end(); 
> std::map<int, void*>::const_iterator iter = begin; 
> 

The next line gets the size() of YET ANOTHER COPY of 'nhash'
returned by getNHash().
> cout << "Size: " << lNodes.getNhash().size() << endl; 
>     while (iter != end) 
>     { 
>       cout << "Key: " << iter->first << endl; 
>       ++iter; 
>     } 
> 
> 
> 
> The output: 
> 
> Size: 3 
> Key: 0x80a2de0 
> 
> or 
> 
> Size: 12 
> Key: 0x80b8ae8 
> Key: 0x80b8ee8 
> Key: 0x80b9170 
> Key: 0x80b9af0 
> Key: 0x80bbb68 
> Key: 0x80bbf68 
> Key: 0x80bc1f0 
> Key: 0x80bcb70 
> 
> There are always some elements missing. Even if the size is 3 just 
> 1 element is printed. 
> Only with hashtables of size 1 I get the correct output. 
> 
> What's wrong?
> 
> Thank you for your help
> 
> Chris
> 


-- 
Anti-spam address, change each 'X' to '.' to reply directly.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]