Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointers to key and value of a map

Reply
Thread Tools

Pointers to key and value of a map

 
 
d.avitabile@gmail.com
Guest
Posts: n/a
 
      05-10-2007
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      05-10-2007
On May 10, 6:45 pm, d.avitab...@gmail.com 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 );



 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-10-2007
On 10 Maj, 10:45, d.avitab...@gmail.com 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

 
Reply With Quote
 
d.avitabile@gmail.com
Guest
Posts: n/a
 
      05-10-2007
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

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-10-2007
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öm

 
Reply With Quote
 
d.avitabile@gmail.com
Guest
Posts: n/a
 
      05-10-2007
On May 10, 11:57 am, Erik Wikström <eri...@student.chalmers.se> wrote:
> 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ö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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
stl map: get the <key,value> pair which has the minimum value Rui Maciel C++ 2 12-01-2009 11:21 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57