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

A

Angus

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,int> and 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
 
S

Saeed Amrollahi

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,int> and 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
 
A

Angus

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.


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,int> and 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
 
U

utab

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.








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 , int> counters; // 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;
// 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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top