Pointers to key and value of a map

D

d.avitabile

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
 
G

Gianni Mariani

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 );
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
D

d.avitabile

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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]);
 
D

d.avitabile

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]);

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top