473,748 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<s tring, string mmap;
typedef std::multimap<s tring, string>::iterat or mapIter;

mmap product_map;
product_map.ins ert( 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.low er_bound("Samsu ng");
mapIter upperB = product_map.upp er_bound("Samsu ng");

for (lowerB; lowerB != upperB; ++lowerB) {
string p = (*lowerB).secon d;
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 5164
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<s tring, string mmap;
typedef std::multimap<s tring, string>::iterat or mapIter;

mmap product_map;
product_map.ins ert( 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.low er_bound("Samsu ng");
mapIter upperB = product_map.upp er_bound("Samsu ng");

for (lowerB; lowerB != upperB; ++lowerB) {
string p = (*lowerB).secon d;
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.c om/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.low er_bound("Samsu ng");
mapIter upperB = product_map.upp er_bound("Samsu ng");
You can combine those using equal_range:

std::pair<mapIt er, mapIterrange = product_map.equ al_range("Samsu ng");

[ ... ]
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
3855
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 service --> MyServiceVersion1_1 MyServiceVersion1_2. I would like to create a factory which could instantiate object using
1
1840
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 long as the default locale is en-US. However, the moment, I change it to say Greek/Portuguese, the parsing logic goofs up. I don't have access to the source code of legacy application to make changes.
6
17812
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
9932
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 result of the formatting should be a string no longer than, say, 3 characters? I know I can easily specify the minimum length, but I am producing a report with columns. I need to
5
2178
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. In particular I looked at pythons hash and lookup functions, so I came up with this (see the code underneath). So, can this code be considered as derived and do I have to put my code under the GPL? I'd like to publish it under something less...
2
4949
by: yesh81 | last post by:
Hi , can anybody write a java program to validate IP Addresses using string tokenizer.
3
1092
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
1425
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++ ) { strCell2 = "<input type=hidden id='Id" + rwCnt + "' name='id" + rwCnt + "' value='" + ID + "' />" + "<input type=hidden id='hidId" + rwCnt + "' name='hidId" + rwCnt + "' value='" + kEntityName + "' />" + "<input type=hidden id='setupType" + ID + "'...
6
2655
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*, const char*> m; m= "Pass";
0
8831
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
3
2215
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.