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:
map<int, void*> HashTable::getN hash() {
return nhash;
}
}
void HashTable::Inse rt(int ky,void* entr) {
nhash.insert(pa ir<int, void*>(ky, entr));
}
....
----------
In another class I use this hashtable:
HashTable lNodes;
const std::map<int, void*>::const_i terator begin =
lNodes.getNhash ().begin(); const std::map<int, void*>::const_i terator end
= lNodes.getNhash ().end(); std::map<int, void*>::const_i terator iter =
begin;
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 3 3663
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:
map<int, void*> HashTable::getN hash() { return nhash; } } void HashTable::Inse rt(int ky,void* entr) { nhash.insert(pa ir<int, void*>(ky, entr)); } ...
----------
In another class I use this hashtable:
HashTable lNodes; const std::map<int, void*>::const_i terator begin = lNodes.getNhash ().begin(); const std::map<int, void*>::const_i terator end = lNodes.getNhash ().end(); std::map<int, void*>::const_i terator iter = begin;
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
Please don't multi-post.
I already answered this in the gnu.g++.help newsgroup.
--
Anti-spam address, change each 'X' to '.' to reply directly.
Christian Christmann wrote: Hi,
I've some problems with iterators on hashtbales.
You're not kidding - although they're maps, not hash tables.
My hashtable.h
class HashTable { map<int, void*> nhash; map<int, void*> getNhash(); ... }
You seem to be returning the nhash member by value here, which is
unusual to say the least. This forces a copy to be made each time
getNhash() is called.
As an aside, I'm assuming this summary is slightly wrong, since both
nhash and getNhash() are private ...
----------
My hashtable.cpp:
map<int, void*> HashTable::getN hash() { return nhash; }
I'm just going to remove the extra closing curly bracket, but in
general
it is nice if the posted code actually compiles (since you're not
asking
about a compiler error, at least).
void HashTable::Inse rt(int ky,void* entr) { nhash.insert(pa ir<int, void*>(ky, entr)); } ...
----------
In another class I use this hashtable:
HashTable lNodes; const std::map<int, void*>::const_i terator begin = lNodes.getNhash ().begin(); const std::map<int, void*>::const_i terator
end = lNodes.getNhash ().end(); std::map<int, void*>::const_i terator iter
= begin;
Mmm, unreadable goo. With permission, I'm going to reformat that so I
have room to hang my comments.
typedef std::map<int,vo id*>::const_ite rator HashIter;
HashIter begin = lNodes.getNhash ().begin();
// make a temporary copy of lNodes' std::map, get the begin iterator
// from it, and then throw that copy away. 'begin' is now an
iterator
// into a destroyed temporary copy of a map.
HashIter end = lNodes.getNhash ().end();
// make another temporary copy of lNodes' std::map, get the end
// iterator from this copy, and then throw the copy away. 'end' is
// now an iterator into another destroyed temporary copy of a map.
HashIter iter = begin;
// copy of an iterator pointing to a destroyed temporary map cout << "Size: " << lNodes.getNhash ().size() << endl;
This creates the third temporary copy of lNodes.nhash so far, but at
least it should give the right result.
while (iter != end) { cout << "Key: " << iter->first << endl; ++iter;
As soon as you dereference an iterator to a deleted map, you're in
'undefined' territory. Simply getting the wrong result is pretty much
the least catastrophic outcome you could hope for.
} }
The output:
<snip - unsurprisingly wrong output>
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?
In case you haven't spotted it from the comments above yet, the problem
is that you're making temporary copies of the map and then throwing
them
away. These anonymous temporaries get destroyed as soon as you hit the
';' at the end of the statement where they're created, ie, at the end
of
"... lNodes.getNhash () ... ;"
If you want to get the size of, and iterators into, an HashTable's
nhash
member rather than making a copy and using that, you need getNhash() to
return a reference rather than a value. Change
map<int, void*> getNhash(); map<int, void*> HashTable::getN hash() { return nhash; }
to
map<int, void*>& getNhash();
map<int, void*>& HashTable::getN hash()
{
return nhash;
}
and everything will work as you expect. No other changes required.
Especially if you're coming from a language (like Java or Python) that
always passes by reference instead of value [1]: remember that C++
allows you to specify whether your arguments and return values have
reference or value semantics. If you want reference semantics, you
need
to put the '&' in to indicate it.
Thank you for your help
np, hth.
Chris
[1] - ok, Java doesn't always pass by reference, just mostly.
> Especially if you're coming from a language (like Java or Python) that always passes by reference instead of value [1]: remember that C++ allows you to specify whether your arguments and return values have reference or value semantics. If you want reference semantics, you need to put the '&' in to indicate it.
Thank you very much for your posting. With your help I could solve
my problem.
np, hth.
Chris This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jhonyxxx |
last post by:
I have the next programa in C++:
#include <iostream.h> // C++ I/O routines
#include <list.h> // The STL list class
#include<stdio.h>
#include <string.h>
typedef struct
{
char nombre;
int edad;
|
by: deancoo |
last post by:
I have gotten into the habit of often using copy along with an insert
iterator. There are scenarios where I process quite a lot of data this way.
Can someone give me a general feel as to how much of a performance hit I'm
taking using this technique versus using 'copy' to copy directly into a
container with elements in place?
Thanks,
d
|
by: codefixer |
last post by:
Hello,
I am trying to understand if ITERATORS are tied to CONTAINERS.
I know the difference between 5 different or 6(Trivial, on SGI).
But what I fail to understand is how can I declare all 5 kinds of
iterators on say a vector.
OR is it that any iterator declared on Vector is Random Iterator which
has the functionality of all the others.
|
by: Lasse Vågsæther Karlsen |
last post by:
I need to merge several sources of values into one stream of values. All
of the sources are sorted already and I need to retrieve the values from
them all in sorted order.
In other words:
s1 =
s2 =
s3 =
for value in ???(s1, s2, s3):
|
by: ma740988 |
last post by:
typedef std::vector < std::complex < double > > complex_vec_type;
// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc, &cc );
}
versus
| |
by: Christian Chrismann |
last post by:
Hi,
I've a runtime problem with STL vectors.
Here is the simplified version of the code:
template <class Tclass A {
...
private:
vector<T*myvector;
typename vector<T*>::itarator mIt;
|
by: John Salerno |
last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
False
print 'hi'
hi
Thanks.
|
by: jmalone |
last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has
AIX and does not want to install Python). The python script is pretty
simple (the only things it imports are sys and socket).
The README file in the Tools/freeze directory of the Python-2.4.4
distribution says the following (and many other things):
Previous...
|
by: desktop |
last post by:
1)
I have this code:
std::list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |