468,257 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

Need Hashmap in C AND C++

Hi,
~
I have found myself in need of some code resembling a Hashmap
~
This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
public static void main(String[] aArgs){
HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
HMSI.put("one", (new Integer(1)));
HMSI.put("zwei", (new Integer(2)));
HMSI.put("tres", (new Integer(3)));
// __
String aK;
int iIxCnt = 0;
System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
Iterator<StringIT = HMSI.keySet().iterator();
while(IT.hasNext()){
aK = IT.next();
System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
++iIxCnt;
}
}
}
~
I was wondering if there are plain ANSI C hashmaps?

Thansk
lbrtchx
Jun 27 '08 #1
15 5125
On Apr 24, 7:18*pm, lbrt...@gmail.com wrote:
*Hi,
~
*I have found myself in need of some code resembling a Hashmap
~
*This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
*public static void main(String[] aArgs){
* HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
* HMSI.put("one", (new Integer(1)));
* HMSI.put("zwei", (new Integer(2)));
* HMSI.put("tres", (new Integer(3)));
// __
* String aK;
* int iIxCnt = 0;
* System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
* Iterator<StringIT = HMSI.keySet().iterator();
* while(IT.hasNext()){
* *aK = IT.next();
* *System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
* *++iIxCnt;
* }
*}}

~
*I was wondering if there are plain ANSI C hashmaps?
They tend to call them hash tables in ordinary C.
Yes, there are lots of them.
Jun 27 '08 #2
On Apr 25, 7:18*am, lbrt...@gmail.com wrote:
*Hi,
~
*I have found myself in need of some code resembling a Hashmap
~
*This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
*public static void main(String[] aArgs){
* HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
* HMSI.put("one", (new Integer(1)));
* HMSI.put("zwei", (new Integer(2)));
* HMSI.put("tres", (new Integer(3)));
// __
* String aK;
* int iIxCnt = 0;
* System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
* Iterator<StringIT = HMSI.keySet().iterator();
* while(IT.hasNext()){
* *aK = IT.next();
* *System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
* *++iIxCnt;
* }
*}}

~
*I was wondering if there are plain ANSI C hashmaps?

*Thansk
*lbrtchx
If it is in C, then is not the right group. you may google or post at
clc. If you are looking for c++, then you may look at the new features
in TR1.

Thanks,
Balaji.
Jun 27 '08 #3
On Apr 24, 7:18*pm, lbrt...@gmail.com wrote:
*Hi,
~
*I have found myself in need of some code resembling a Hashmap
~
*This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
*public static void main(String[] aArgs){
* HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
* HMSI.put("one", (new Integer(1)));
* HMSI.put("zwei", (new Integer(2)));
* HMSI.put("tres", (new Integer(3)));
// __
* String aK;
* int iIxCnt = 0;
* System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
* Iterator<StringIT = HMSI.keySet().iterator();
* while(IT.hasNext()){
* *aK = IT.next();
* *System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
* *++iIxCnt;
* }
*}}

~
*I was wondering if there are plain ANSI C hashmaps?
This one has a friendly hashmap name:
http://www2.informatik.hu-berlin.de/...src_index.html
Jun 27 '08 #4
On Apr 25, 5:18 am, lbrt...@gmail.com wrote:
Hi,
~
I have found myself in need of some code resembling a Hashmap
~
This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
public static void main(String[] aArgs){
HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
HMSI.put("one", (new Integer(1)));
HMSI.put("zwei", (new Integer(2)));
HMSI.put("tres", (new Integer(3)));
// __
String aK;
int iIxCnt = 0;
System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
Iterator<StringIT = HMSI.keySet().iterator();
while(IT.hasNext()){
aK = IT.next();
System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
++iIxCnt;
}
}}

~
I was wondering if there are plain ANSI C hashmaps?

Thansk
lbrtchx
In C++ you might find map/multimap, which are implemented in terms of
balanced trees as far as I know (red/black trees).
In plain C you won't find anything like this. You have to write them
by your own.
Jun 27 '08 #5
lb*****@gmail.com wrote:
I was wondering if there are plain ANSI C hashmaps?
Standard C has no data containers whatsoever, so no such luck.
Jun 27 '08 #6
Sam
lb*****@gmail.com writes:
Hi,
~
I have found myself in need of some code resembling a Hashmap
std::map

Not actually a hash-based map, but you won't care.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIEbxKx9p3GYHlUOIRAtV1AJ4+28z4mFqkEhtBMD9XqQ EgmnQqiQCfYMI5
6BtpLJMFN1zq1cDKX/aY3vM=
=xhqa
-----END PGP SIGNATURE-----

Jun 27 '08 #7
Sam wrote, On 25/04/08 12:11:
lb*****@gmail.com writes:
> Hi,
~
I have found myself in need of some code resembling a Hashmap

std::map

Not actually a hash-based map, but you won't care.
To find out whether this and the other C++ answers are correct ask in
comp.lang.c++ where C++ is topical and there are rather more C++
experts. It is not topical here in comp.lang.c
--
Flash Gordon
Jun 27 '08 #8
On Apr 25, 4:18 am, lbrt...@gmail.com wrote:
Hi,
~
I have found myself in need of some code resembling a Hashmap
~
This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
public static void main(String[] aArgs){
HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
HMSI.put("one", (new Integer(1)));
HMSI.put("zwei", (new Integer(2)));
HMSI.put("tres", (new Integer(3)));
// __
String aK;
int iIxCnt = 0;
System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
Iterator<StringIT = HMSI.keySet().iterator();
while(IT.hasNext()){
aK = IT.next();
System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
++iIxCnt;
}
}}

~
I was wondering if there are plain ANSI C hashmaps?

Thansk
lbrtchx
For C++ you can try std::map.
Regards.
Jun 27 '08 #9
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Sam wrote, On 25/04/08 12:11:
>lb*****@gmail.com writes:
>> Hi,
~
I have found myself in need of some code resembling a Hashmap

std::map

Not actually a hash-based map, but you won't care.

To find out whether this and the other C++ answers are correct ask in
comp.lang.c++ where C++ is topical and there are rather more C++
experts. It is not topical here in comp.lang.c
Seriously, do you not get bored constantly elevating yourself into the
spotlight with such trite, repetitive "advice"?

Jun 27 '08 #10
Richard wrote, On 25/04/08 22:44:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>Sam wrote, On 25/04/08 12:11:
>>lb*****@gmail.com writes:

Hi,
~
I have found myself in need of some code resembling a Hashmap
std::map

Not actually a hash-based map, but you won't care.
To find out whether this and the other C++ answers are correct ask in
comp.lang.c++ where C++ is topical and there are rather more C++
experts. It is not topical here in comp.lang.c

Seriously, do you not get bored constantly elevating yourself into the
spotlight with such trite, repetitive "advice"?
Are you suggesting that C++ is topical here?
Don't you get bored complaining when people are redirected to a more
appropriate place?
--
Flash Gordon
Jun 27 '08 #11

<lb*****@gmail.comwrote in message
I was wondering if there are plain ANSI C hashmaps?
In C++ there is one in the standard template library. In C you have to use a
third party one, and as far as I know nothing has become a de facto
standard. So the alternative uis to write one yourself.

There's a hashtable on my website, in the Basic Algorithms example code.
However it is meant as teaching code about how the hash table works.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #12
lb*****@gmail.com wrote:
>
.... snip ...
>
I was wondering if there are plain ANSI C hashmaps?
Yes. See hashlib at: <http://cbfalconer.home.att.net/download/>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #13
Juha Nieminen wrote:
lb*****@gmail.com wrote:
>I was wondering if there are plain ANSI C hashmaps?

Standard C has no data containers whatsoever, so no such luck.
I thought I answered this already. At any rate, there are
libraries written in Standard C, and thus highly portable. One is
my hashlib package, released under GPL. See:

<http://cbfalconer.home.att.net/download/>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #14
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:ei************@news.flash-gordon.me.uk...
Sam wrote, On 25/04/08 12:11:
>lb*****@gmail.com writes:
>> Hi,
~
I have found myself in need of some code resembling a Hashmap

std::map

Not actually a hash-based map, but you won't care.

To find out whether this and the other C++ answers are correct ask in
comp.lang.c++ where C++ is topical and there are rather more C++ experts.
It is not topical here in comp.lang.c
It's not topical in news:comp.lang.c++ either. If it needs to work in both
C and C++, then a C implementation is a good idea. You can find hundreds of
them (like the one that I found upthread) with a web search.

I can't really think of an appropriate newsgroup to find an answer for the
question, which is why I went ahead and answered it. The FAQ answer of
news:comp.sources.wanted is really pointing to a dead newsgroup that nobody
bothers with any more (I see one valid post in the past several months).
** Posted from http://www.teranews.com **
Jun 27 '08 #15
In article <65**********************************@m36g2000hse. googlegroups.com>,
<lb*****@gmail.comwrote:
Hi,
~
I have found myself in need of some code resembling a Hashmap
~
This is easily done in Java this way:
~
import java.util.*;

// __
public class JMith00Test{
public static void main(String[] aArgs){
HashMap<String, IntegerHMSI = new HashMap<String, Integer>();
HMSI.put("one", (new Integer(1)));
HMSI.put("zwei", (new Integer(2)));
HMSI.put("tres", (new Integer(3)));
// __
String aK;
int iIxCnt = 0;
System.out.println("// __ HMSI.size(): |" + HMSI.size() + "|");
Iterator<StringIT = HMSI.keySet().iterator();
while(IT.hasNext()){
aK = IT.next();
System.out.println("// __ |" + iIxCnt + "|" + aK + "|" +
HMSI.get(aK) + "|");
++iIxCnt;
}
}
}
~
I was wondering if there are plain ANSI C hashmaps?
There is no hash map that is part of "ANSI C" (nor any of the more
recent standards like ISO C ). However, there are countless
implementation of hash map for C as a library. Some of them might be
extermely easily available on your particular platform.
My libc has more than one.

In C++, there are two things that should be your first port of call.
std::map is not a hash map but a balanced tree map. Unless you
understand the differences between hash map and balanced tree maps
( e.g. best case, typical and importantly worse case) and you know
that your particular application needs hash map this might very well
do the job you need.

#include <map>
std::map< std::string, intHMSI;
HMSI["one"] = 1;
HMSI["zwei"] = 2;
HMSI["tres"] = 3;
// ...
for(
std::map< std::string, int >::const_iterator it = HMSI.begin();
it != HMSI.end() ; ++it )
{
std::cout << it->first << " = " << it->second <<
std::endl;
}

If you need a hash map, C++ TR1 has a hash map:

#include <tr1/unordered_map>
std::tr1::unordered_map< std::string, int HMSI;
//... the rest is identical
Yan
Jun 27 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Welson Sun | last post: by
2 posts views Thread by dougjrs | last post: by
4 posts views Thread by David | last post: by
2 posts views Thread by xor | last post: by
4 posts views Thread by panos100m | last post: by
reply views Thread by lbrtchx | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.