473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Should I implement this using string hash or multimap..

I need to implement the following. Shoul I use multimap or write a string
hash class? ie

Brand Product
==========================
Samson Television
Samsung Television
Samsung VCR
Samsung DVD Player
Samsunk Television

The brand is the key and the product is the data.

If I use multimap, then I can implement it using something like this

typedef std::multimap<string, string mmap;
typedef std::multimap<string, string>::iterator mapIter;

mmap product_map;
product_map.insert( pair<string, stringstring a(Samsung), string
b(Television));
..
..
..
To find the product in brand, all I have to do is iterate using lower and
upper bound.

mapIter lowerB = product_map.lower_bound("Samsung");
mapIter upperB = product_map.upper_bound("Samsung");

for (lowerB; lowerB != upperB; ++lowerB) {
string p = (*lowerB).second;
if ( strcmp (p.c_str(), "VCR") == 0 ) ) {
//do something
} else {
//do something
}
}

Assume that I have a lot of data. Is this implementation efficient? I know
using string as the key is not a good idea. Is there a better way to do
this? Is the performance better using hash? How do I implent the hash
function.

Thanks in advance..

-Jonny
Sep 15 '06 #1
3 5150
Hi,

You might want to look into:

http://www.sgi.com/tech/stl/hash_multimap.html
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Jonay Aloat" <jo****@comcast.netwrote in message
news:27******************************@comcast.com. ..
>I need to implement the following. Shoul I use multimap or write a string
hash class? ie

Brand Product
==========================
Samson Television
Samsung Television
Samsung VCR
Samsung DVD Player
Samsunk Television

The brand is the key and the product is the data.

If I use multimap, then I can implement it using something like this

typedef std::multimap<string, string mmap;
typedef std::multimap<string, string>::iterator mapIter;

mmap product_map;
product_map.insert( pair<string, stringstring a(Samsung), string
b(Television));
.
.
.
To find the product in brand, all I have to do is iterate using lower and
upper bound.

mapIter lowerB = product_map.lower_bound("Samsung");
mapIter upperB = product_map.upper_bound("Samsung");

for (lowerB; lowerB != upperB; ++lowerB) {
string p = (*lowerB).second;
if ( strcmp (p.c_str(), "VCR") == 0 ) ) {
//do something
} else {
//do something
}
}

Assume that I have a lot of data. Is this implementation efficient? I know
using string as the key is not a good idea. Is there a better way to do
this? Is the performance better using hash? How do I implent the hash
function.

Thanks in advance..

-Jonny

Sep 15 '06 #2
Moonlit wrote:
Hi,

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>


Brian
Sep 15 '06 #3
In article <27******************************@comcast.com>,
jo****@comcast.net says...

[ ... ]
To find the product in brand, all I have to do is iterate using lower and
upper bound.

mapIter lowerB = product_map.lower_bound("Samsung");
mapIter upperB = product_map.upper_bound("Samsung");
You can combine those using equal_range:

std::pair<mapIter, mapIterrange = product_map.equal_range("Samsung");

[ ... ]
Assume that I have a lot of data. Is this implementation efficient? I know
using string as the key is not a good idea. Is there a better way to do
this? Is the performance better using hash? How do I implent the hash
function.
multimap lookups take approximately log2(N) operations. Hashing is
normally about linear on the length of a key string. IOW, the two depend
on entirely different factors, making it difficult to predict which will
have better performance except in extreme cases.

Predicting which will be faster requires something much more specific
than "a lot of data". To different people, that might indicate sizes
that vary by several orders of magnitude. OTOH, both hash tables and
multimaps are oriented primarily toward in-memory collections, and
unless you're working on some sort of supercomputer, chances are that
you don't have enough memory for a large enough collection to really
strongly favor a hash table.

My advice would be to start with a multimap since you already seem
comfortable with that, and only worry about using a hash table in the
unlikely event that profiling shows the multimap is inadequate.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sebastien Degardin | last post by:
Hello, I need to use a Factory pattern to create services class. i have an interface --> MyService i can have an abstract class --> MyAbstractService i have several concrete class for this...
1
by: Prasad Dabak | last post by:
Hello, I have a legacy unmanaged application that returns property=value pairs separated by chr(252)and I am trying to parse this output from C# using string.split method. This works fine as...
6
by: Frank King | last post by:
Hi, I am looking for a hash function to map string to string in VB. Could somebody give me some informtion? Thank you very much. fk
1
by: Alan Foxmore | last post by:
Hello all, Is it possible to use String.Format() to specify a maximum length for a formatted item? For example, let's say I have: String.Format("{0}", "FREDDY"); How can I specify that the...
5
by: Mathias Panzenboeck | last post by:
Hi. I wrote a small hashlib for C. Because I'm new to hashes I looked at pythons implementation and reused *some* of the code... or more the mathematical "hash-function", not really the code. ...
2
by: yesh81 | last post by:
Hi , can anybody write a java program to validate IP Addresses using string tokenizer.
3
by: Arcticool | last post by:
Are there any tools besides Visual Studio that I should be using to learn C#. What utilities do you folks use and think could be helpful to somebody trying to grasp all this? Thanks, Jack
4
by: Dev | last post by:
Can replacing this with stringbuilder advisable. Or since iteration is less only 20 times, the time taken by using strings and string builder will be the same. for( int i = 0; i< 20; i++ ) { ...
6
by: Man4ish | last post by:
How to use char* instead of using string as key and value. map <string, string> m; m= "Pass"; m= "Fail"; When i try to use char* as key value there is memory leak. map <const char*,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.