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

Discussion in 'C++' started by Angus, May 3, 2008.

  1. Angus

    Angus Guest

    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
    Angus, May 3, 2008
    #1
    1. Advertising

  2. Re: map question - need to find a string from an int but also needthe int from the string?

    On May 3, 1:09 pm, "Angus" <> wrote:
    > 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
    Saeed Amrollahi, May 3, 2008
    #2
    1. Advertising

  3. Angus

    Angus Guest

    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" <> wrote in message
    news:...
    On May 3, 1:09 pm, "Angus" <> wrote:
    > 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
    Angus, May 3, 2008
    #3
  4. Angus

    utab Guest

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

    On May 3, 2:03 pm, "Angus" <> wrote:
    > 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" <> wrote in message
    >
    > news:...
    > On May 3, 1:09 pm, "Angus" <> wrote:
    >
    >
    >
    > > 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


    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...
    utab, May 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,198
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  3. aling
    Replies:
    8
    Views:
    933
    Jim Langston
    Oct 20, 2005
  4. Replies:
    9
    Views:
    418
    James Kanze
    Apr 17, 2007
  5. albert kao
    Replies:
    12
    Views:
    581
    Roedy Green
    Oct 7, 2011
Loading...

Share This Page