473,378 Members | 1,542 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,378 software developers and data experts.

map question - need to find a string from an int but also need the int from the string?

I have a map between an integer value - call it a deviceID and a device
name - call it devicename.

If I use a map I can either use the int or the string as the key. But
which? If I use for example the deviceid as the key, then if I need to find
the deviceID from the devicename then I need to iterate through the whole
map. I am worried about performance of this.

I setup the map at program startup - and it is static. So I wondered about
haveing a map<string,intand a map<int,string>. But it doesn't seem so
elegant - although maybe most efficient way.

Are there any other way of me achieving my goal of high performance in
lookups both ways?

Angus
Jun 27 '08 #1
4 1579
Angus ha scritto:
I have a map between an integer value - call it a deviceID and a device
name - call it devicename.

If I use a map I can either use the int or the string as the key. But
which? If I use for example the deviceid as the key, then if I need to find
the deviceID from the devicename then I need to iterate through the whole
map. I am worried about performance of this.

I setup the map at program startup - and it is static. So I wondered about
haveing a map<string,intand a map<int,string>. But it doesn't seem so
elegant - although maybe most efficient way.

Are there any other way of me achieving my goal of high performance in
lookups both ways?

Angus

One solution may be boost bimap:

http://www.boost.org/doc/libs/1_35_0...tml/index.html

--
Marco
Jun 27 '08 #2
On May 3, 1:09*pm, "Angus" <nos...@gmail.comwrote:
I have a map between an integer value - call it a deviceID and a device
name - call it devicename.

If I use a map I can either use the int or the string as the key. *But
which? *If I use for example the deviceid as the key, then if I need to find
the deviceID from the devicename then I need to iterate through the whole
map. *I am worried about performance of this.

I setup the map at program startup - and it is static. *So I wondered about
haveing a map<string,intand a map<int,string>. *But it doesn't seem so
elegant - although maybe most efficient way.

Are there any other way of me achieving my goal of high performance in
lookups both ways?

Angus
Hi

I think using two maps is a straightforward solution. Except loading
data into two data structures rather than one, there is no performance
penalty. Of course there is one point: Almost the DeviceId should be
unique and it is a key, how about Devicename. Is it unique?

Regards,
- Saeed Amrollahi
Jun 27 '08 #3
Yes both are unique.

I am interested in using Boost. It does seem to add on some very useful
additions to just using the standard library. But I am not familiar with
it - I use VC++ v6 which I believe has issues with it. So doing this
without boost is pragmatic solution for now.
"Saeed Amrollahi" <s_*********@yahoo.comwrote in message
news:52**********************************@z24g2000 prf.googlegroups.com...
On May 3, 1:09 pm, "Angus" <nos...@gmail.comwrote:
I have a map between an integer value - call it a deviceID and a device
name - call it devicename.

If I use a map I can either use the int or the string as the key. But
which? If I use for example the deviceid as the key, then if I need to
find
the deviceID from the devicename then I need to iterate through the whole
map. I am worried about performance of this.

I setup the map at program startup - and it is static. So I wondered about
haveing a map<string,intand a map<int,string>. But it doesn't seem so
elegant - although maybe most efficient way.

Are there any other way of me achieving my goal of high performance in
lookups both ways?

Angus
Hi

I think using two maps is a straightforward solution. Except loading
data into two data structures rather than one, there is no performance
penalty. Of course there is one point: Almost the DeviceId should be
unique and it is a key, how about Devicename. Is it unique?

Regards,
- Saeed Amrollahi
Jun 27 '08 #4
On May 3, 2:03 pm, "Angus" <nos...@gmail.comwrote:
Yes both are unique.

I am interested in using Boost. It does seem to add on some very useful
additions to just using the standard library. But I am not familiar with
it - I use VC++ v6 which I believe has issues with it. So doing this
without boost is pragmatic solution for now.

"Saeed Amrollahi" <s_amroll...@yahoo.comwrote in message

news:52**********************************@z24g2000 prf.googlegroups.com...
On May 3, 1:09 pm, "Angus" <nos...@gmail.comwrote:
I have a map between an integer value - call it a deviceID and a device
name - call it devicename.
If I use a map I can either use the int or the string as the key. But
which? If I use for example the deviceid as the key, then if I need to
find
the deviceID from the devicename then I need to iterate through the whole
map. I am worried about performance of this.
I setup the map at program startup - and it is static. So I wondered about
haveing a map<string,intand a map<int,string>. But it doesn't seem so
elegant - although maybe most efficient way.
Are there any other way of me achieving my goal of high performance in
lookups both ways?
Angus

Hi

I think using two maps is a straightforward solution. Except loading
data into two data structures rather than one, there is no performance
penalty. Of course there is one point: Almost the DeviceId should be
unique and it is a key, how about Devicename. Is it unique?

Regards,
- Saeed Amrollahi
Maybe a dirty trick but maybe not what you are looking for directly...

taking the pairs of a map into a vector, reversed...

a piece of code to give you an idea:
....
string s;
map<string , intcounters; // store each word and an associated
counter
vector<pair<string, int vec;

// read the input, keeping track of each word and how often we see
it
while (cin >s)
++counters[s];
// write the words and associated counts
#ifdef _MSC_VER
for (std::map<string, int>::const_iterator it = counters.begin();
#else
for (map<string, int>::const_iterator it = counters.begin();
#endif
it != counters.end(); ++it)
{
//cout << it->first << "\t" << it->second << endl;
vec.push_back(pair<string, int>(it->first, it->second));

}
....
After that you can do some search in the vector maybe however vectors
are not for efficient look up...
Jun 27 '08 #5

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
2
by: Benji99 | last post by:
Hi guys, I'm starting to learn Python and so far am very impressed with it's possibilities. I do however need some help with certain things I'm trying to do which as of yet haven't managed to find...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
1
by: 848lu | last post by:
hey i really need help...i got this code....basically im suppose to make a calender that allows a user to type in month and year .... and the calander displays it on the scree using...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
3
by: akshaycjoshi | last post by:
IT"S A LONG QUESTION.PLEASE BE PATIENT TO READ IT. I HAVE EXPLAINED IT TO MY MAXIMUM CAPACITY. I am making one app. in which i need to create a seperate access database for each day. Its a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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

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.