|
From: David M. <ci...@ya...> - 2003-06-30 17:52:44
|
Cool.
The only thing I was planning on using was the [] so I
could access the function pointers in the first map
easily
(will do this more often, the reverse look-up will only
be
used by admin functions setting up the connection between
command name and function pointer)
--- Ioannis Vranos <iv...@em...> wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On
> Behalf
> > Of David McKen
> > Sent: Monday, June 30, 2003 6:27 PM
> > To: Dev-C Mailing List
> > Subject: [Dev-C++] Reverse Map lookup.
> >
> >
> > Ok, here is a question for all you STL users out
> there.
> >
> > I have a map which uses a string as the key and the
> data
> > object is a function pointer (I am yet to implement
> this so
> > don't ask me how I did it). What I would need (please
> don't
> > ask me why, it is the request of another member of
>our
> > team) but I also need to do a reverse-lookup (for a
> given
> > function pointer address what strings are assigned).
> >
> > For the curious this is for a dynamic command system.
> When
> > the user enters a command we look up what function is
> > assigned to that command. For now you can assume that
> it is
> > a 1:1 relationship, no more than one command is
> mapped
> to a
> > particular function. This might change in the future
> > (should we start implementing aliases) so a solution
> that
> > would solve this as well would be quite helpful.
>
>
> multimap for more than 1:1.
>
>
>
> > So far the only solutions I see is to set up 2 maps
> (2nd
> > one for the reverse lookup would have to be a
> multimap
> if
> > more than one command can be mapped to a particular
> > function)
>
>
> Yes. There is a std::multimap.
>
>
> >. To make sure everything is kept in sync I would
> > probably have to encapsulate it and over-ride some
> > operators.
>
>
> If you are not very time concerned (I am using maps in
> this style myself and
> haven't found it to be very slow) you can use a loop to
> check each one of
> the addresses. Look what I am doing in a program of
> mine:
>
>
> > map<unsigned, double> al;
> >
> > // Ls sorted
> > vector<double>L;
> >
> > // ...
> >
> >
> > The double are floating point numbers, the same in both
> > cases. In L they are
> > sorted though:
> >
> >
> > for(vector<unsigned>::size_type i=0; i<E.size(); ++i)
> > {
> > for(vector<unsigned>::size_type j=0; j<A[i].size();
> > ++j)
> > {
> > al[ A[i][j] ]= something;
> >
> > L.push_back(al[ A[i][j] ]);
> > }
> > }
> >
> >
> >
> > sort(L.begin(), L.end());
> >
> >
> > system("cls");
> >
> >
> >
> > for(map<unsigned, double>::size_type i=1;
> i<=al.size();
> > ++i)
> > {
> > cout<<i<<": "<<al[i]<<"\t\t\t";
> >
> > cout<<L[i-1]<<": ";
> >
> > for(map<unsigned, double>::size_type j=1;
> > j<=al.size(); ++j)
> > {
> > if(L[i-1]==al[j])
> > {
> > cout<<j<<endl;
> > break;
> > }
> > }
> >
> > }
> >
> >
> >
> > Check the last for loop inside the same for loop. I
> > search the value L[i-1]
> > (sorted vector) inside the map and display the unsigned
> > corresponding to it.
> > It doesn't take long.
> >
> >
> > If you can't afford it, then you will have to use a
> > multimap and a map and
> > keep them synchronized. I do not see any need to
> overload
> > anything though.
=====
Signed
David Mcken
Life Sucks
Live with it
__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com
|