Re: Pointers to key and value of a map
On 10 Maj, 11:54, d.avitab...@gmail.com 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=F6m