Pointers to key and value of a map

Discussion in 'C++' started by d.avitabile@gmail.com, May 10, 2007.

  1. Guest

    Hi everybody,

    I have defined a standard map as follows

    map<int,double> nonzeroEntries;

    because I wanted to take advantage of the container class. Now I have
    a function with the following interface

    myfunction( int * theIntegersInTheMap, double * theDoublesInTheMap);

    therefore I need pointers to the key list and to the value list. Is
    there any smart way to do this? At the moment I create two pointers

    int * theIntegers = new int[sizeOfTheList];

    double * theIntegers = new double[sizeOfTheList];

    and fill them in, but this solution seems a bit redundant.

    In case you didn't get it... I'm a total newbie in maps.

    Thanks in advance
     
    , May 10, 2007
    #1
    1. Advertising

  2. On May 10, 6:45 pm, wrote:
    > Hi everybody,
    >
    > I have defined a standard map as follows
    >
    > map<int,double> nonzeroEntries;
    >
    > because I wanted to take advantage of the container class. Now I have
    > a function with the following interface
    >
    > myfunction( int * theIntegersInTheMap, double * theDoublesInTheMap);
    >
    > therefore I need pointers to the key list and to the value list. Is
    > there any smart way to do this? At the moment I create two pointers
    >
    > int * theIntegers = new int[sizeOfTheList];
    >
    > double * theIntegers = new double[sizeOfTheList];
    >
    > and fill them in, but this solution seems a bit redundant.
    >
    > In case you didn't get it... I'm a total newbie in maps.


    Try rewriting myfunction to use an iterator.

    myfunction( map<int,double>::iterator );
     
    Gianni Mariani, May 10, 2007
    #2
    1. Advertising

  3. On 10 Maj, 10:45, wrote:
    > Hi everybody,
    >
    > I have defined a standard map as follows
    >
    > map<int,double> nonzeroEntries;
    >
    > because I wanted to take advantage of the container class. Now I have
    > a function with the following interface
    >
    > myfunction( int * theIntegersInTheMap, double * theDoublesInTheMap);
    >
    > therefore I need pointers to the key list and to the value list. Is
    > there any smart way to do this? At the moment I create two pointers
    >
    > int * theIntegers = new int[sizeOfTheList];
    >
    > double * theIntegers = new double[sizeOfTheList];
    >
    > and fill them in, but this solution seems a bit redundant.
    >
    > In case you didn't get it... I'm a total newbie in maps.


    Sorry, but there's no easy way to do this, neither the keys not the
    values are stored consecutive int the map (since it's node-based).

    However if it is you who have written myfunction() and you want the
    lists so that you can iterate over easily then you should use
    std::map<int, double>::iterator instead.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 10, 2007
    #3
  4. Guest

    Unfortunately, I have no control on the funcion "myfunction", despite
    the name I had given in the example...
    I can not touch, nor modify myfunction's interface.

    I am doing this at the moment

    int * theIntegers = new int[maxSizeOfTheList];
    double * theIntegers = new double[maxSizeOfTheList];
    int sizeOfTheList = 0
    // Building indices and values
    for ( map<int,double>::iterator it=nonzeroEntries.begin() ; it !
    = nonzeroEntries.end(); it++ ) {
    theIntegers[sizeOfTheList] = (*it).first;
    theDoubles[sizeOfTheList] = (*it).second;
    ++sizeOfTheList;
    }

    Can you just confirm that what I wrote is not absolutely inefficient?

    Thanks again guys.

    Daniele
     
    , May 10, 2007
    #4
  5. On 10 Maj, 11:54, wrote:
    > Unfortunately, I have no control on the funcion "myfunction", despite
    > the name I had given in the example...


    Please, quote the text you are replying to.

    > I can not touch, nor modify myfunction's interface.
    >
    > I am doing this at the moment
    >
    > int * theIntegers = new int[maxSizeOfTheList];
    > double * theIntegers = new double[maxSizeOfTheList];
    > int sizeOfTheList = 0
    > // Building indices and values
    > for ( map<int,double>::iterator it=nonzeroEntries.begin() ; it !
    > = nonzeroEntries.end(); it++ ) {
    > theIntegers[sizeOfTheList] = (*it).first;
    > theDoubles[sizeOfTheList] = (*it).second;
    > ++sizeOfTheList;
    > }
    >
    > Can you just confirm that what I wrote is not absolutely inefficient?


    No, it's not horribly inefficient, but it can be made better (if
    perhaps not more efficient) by using vectors instead:

    vector<int> keys;
    vector<double> values;
    key.reserve(nonzeroEntries.size());
    values.reserve(nonzeroEntries.size());
    for (map<int, double>::iterator it = nonzeroEntries.begin(); it !=
    nonzeroEntries.end(); ++it)
    {
    keys.push_back(it->first);
    values.push_back(it->first);
    }

    Unless the size nonzeroEntries can be really large you probably wont
    notice much difference if you delete the reserve()-calls. By using
    vectors you don't have to worry about freeing the memory used by the
    arrays, but you get all the functionality. You can then call
    myfunction like this:

    myfunction(&keys[0], &values[0]);

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 10, 2007
    #5
  6. Guest

    On May 10, 11:57 am, Erik Wikström <> wrote:
    > On 10 Maj, 11:54, wrote:
    >
    > > Unfortunately, I have no control on the funcion "myfunction", despite
    > > the name I had given in the example...

    >
    > Please, quote the text you are replying to.
    >
    >
    >
    > > I can not touch, nor modify myfunction's interface.

    >
    > > I am doing this at the moment

    >
    > > int * theIntegers = new int[maxSizeOfTheList];
    > > double * theIntegers = new double[maxSizeOfTheList];
    > > int sizeOfTheList = 0
    > > // Building indices and values
    > > for ( map<int,double>::iterator it=nonzeroEntries.begin() ; it !
    > > = nonzeroEntries.end(); it++ ) {
    > > theIntegers[sizeOfTheList] = (*it).first;
    > > theDoubles[sizeOfTheList] = (*it).second;
    > > ++sizeOfTheList;
    > > }

    >
    > > Can you just confirm that what I wrote is not absolutely inefficient?

    >
    > No, it's not horribly inefficient, but it can be made better (if
    > perhaps not more efficient) by using vectors instead:
    >
    > vector<int> keys;
    > vector<double> values;
    > key.reserve(nonzeroEntries.size());
    > values.reserve(nonzeroEntries.size());
    > for (map<int, double>::iterator it = nonzeroEntries.begin(); it !=
    > nonzeroEntries.end(); ++it)
    > {
    > keys.push_back(it->first);
    > values.push_back(it->first);
    >
    > }
    >
    > Unless the size nonzeroEntries can be really large you probably wont
    > notice much difference if you delete the reserve()-calls. By using
    > vectors you don't have to worry about freeing the memory used by the
    > arrays, but you get all the functionality. You can then call
    > myfunction like this:
    >
    > myfunction(&keys[0], &values[0]);
    >
    > --
    > Erik Wikström


    I will use your implementation. Since I need to use the vector<int>
    keys and vector<double> values in a for loop, I will also call a
    values.clear() and keys.clear() at the end the loop.

    Thanks again for all your suggestions.

    Ciao

    Daniele
     
    , May 10, 2007
    #6
    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. Patrick Guio
    Replies:
    6
    Views:
    3,262
    chris
    Oct 20, 2004
  2. Mize-ze
    Replies:
    4
    Views:
    539
    Robert Klemme
    Jan 1, 2007
  3. Rui Maciel
    Replies:
    2
    Views:
    3,249
    AndrewDover
    Dec 1, 2009
  4. Une bévue
    Replies:
    5
    Views:
    173
    Une bévue
    Aug 10, 2006
  5. Antonio Quinonez
    Replies:
    2
    Views:
    191
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page