Hello
I have a big problem with hash_map in stl.
I'm trying to create hash_map<const char *, char *> map
to keep key => val pairs as 4 byte C "strings".
Everything is ok when i do let's say
my_hash["abc"] = "efg";
my_hash["aa"] = "bb";
and then
for (it = my_hash.begin(); it != my_hash.end(); ++it)
printf("%s %s\n", it->first, it->second);
it will print 2 entries
The problem starts when i try to push values to my_hash that
i read from a file. Here's a bit of code:
for (i = 0; i < N2; i++)
{
fread_unlocked(mro1, sizeof(char), 4, stdin);
fread_unlocked(mro2, sizeof(char), 4, stdin);
m_hash[mro1] = (char *) malloc(4*sizeof(char));
memcpy(m_hash[mro1], mro2, 4);
/*
or like this:
m_hash[mro1] = mro2
*/
}
mro1 and mro2 are 4*sizeof(char) bytes malloced vectors
for (it = my_hash.begin(); it != my_hash.end(); ++it)
printf("%s %s\n", it->first, it->second);
will print only one, last entry i read from file. I figured out that it no
longer keeps "strings" as keys but pointers. Can someone tell me how to make
it work like my_hash["dljfj"] = "dlkfjkjdf"; ?
gibffe 2 2054
"gibffe" <gi****@o2.pl> wrote in message
news:cl**********@achot.icm.edu.pl... I'm trying to create hash_map<const char *, char *> map to keep key => val pairs as 4 byte C "strings".
You are not taking advantage of std::string and struggling with C-style
strings.
Everything is ok when i do let's say my_hash["abc"] = "efg"; my_hash["aa"] = "bb"; and then for (it = my_hash.begin(); it != my_hash.end(); ++it) printf("%s %s\n", it->first, it->second); it will print 2 entries
The problem starts when i try to push values to my_hash that i read from a file. Here's a bit of code:
for (i = 0; i < N2; i++) { fread_unlocked(mro1, sizeof(char), 4, stdin); fread_unlocked(mro2, sizeof(char), 4, stdin);
m_hash[mro1] = (char *) malloc(4*sizeof(char));
So you are using the same key (the value of mro1, which is a memory address)
for everything you read. You are overwriting the old value each time you do
this.
Since you don't free the previously malloced 4 bytes, you also leak memory
here.
memcpy(m_hash[mro1], mro2, 4); /* or like this:
m_hash[mro1] = mro2
If you did that, you would not leak memory, but still keep one key/value
pair (mro1/mro2) in the container. */ }
mro1 and mro2 are 4*sizeof(char) bytes malloced vectors
Since you don't malloc mro1 per key, your key is the same for everything you
read.
for (it = my_hash.begin(); it != my_hash.end(); ++it) printf("%s %s\n", it->first, it->second);
will print only one, last entry i read from file. I figured out that it no longer keeps "strings" as keys but pointers. Can someone tell me how to
make it work like my_hash["dljfj"] = "dlkfjkjdf"; ?
Use std::string, not 'char *':
#include <string>
/* ... */
typedef hash_map<std::string, std::string> MyMap;
MyMap m_hash;
m_hash["dljfj"] = "dlkfjkjdf";
Ali Use std::string, not 'char *':
#include <string> /* ... */ typedef hash_map<std::string, std::string> MyMap;
MyMap m_hash; m_hash["dljfj"] = "dlkfjkjdf";
Obviously this is the correct advice. But the thing to add is that some
implementations of hash_map do not have a hash function defined for
std::string. Therefore the OP might need to do add that themselves. Since
hash_map is non-standard the way to do that will vary, consult your
documentation. The other option would be to switch to std::map (and use
std::string of course).
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel Heiserer |
last post by:
Hello,
I used a unique associative container such as:
//---------------
map<vector<int>,double> X;
//---------------
In general I fill X with millions of entries.
Sorting plays no role...
|
by: Murali |
last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL
hash map.
A couple of ways to do this is
1. create a struct with two unsigned ints and use that as key (write my own...
|
by: pembed2003 |
last post by:
Hi All,
Does C++/STL have hashtable where I can do stuff like:
Hashtable h<int>;
h.store("one",1);
h.store("two",2);
and then later retrieve them like:
|
by: Generic Usenet Account |
last post by:
Consider two entities A and B such that there is a 1:n association
between them. I mean that associated with each instance of A there are
up to n instances of B. Currently in our software we are...
|
by: anjangoswami06 |
last post by:
Hi,
I am interested to know how it is possible to update the data type with
"insert" with stl hash_map. Suppose we have,
hash_map<const char *, MyDataType, hash_compare<const char*,...
|
by: jayesah |
last post by:
Hi All,
I am developing my code with Apache stdcxx. I am bound to use STL of
Apache only.
Now today I need hash_map in code but as I learned, it is not
available in Apache since it is not...
|
by: Hunk |
last post by:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl...
|
by: Amit Bhatia |
last post by:
Hi,
I am trying to use hash maps from STL on gcc 3.3 as follows:
#ifndef NODE_H
#define NODE_H
#include <ext/hash_map>
#include "node_hasher.h"
class Node;
typedef hash_map<pair<int,int>,...
|
by: mono12 |
last post by:
Hi!
I have written code using STL hash_map in c++ and am running into memory issues. I have two kinds of hash_maps:
A <int,int> which stores ~5million key-value pairs, and needs to be accessed...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
| |