> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of David McKen
> Sent: Monday, June 30, 2003 6:27 PM
> To: Dev-C Mailing List
> Subject: [Dev-C++] Reverse Map lookup.
>=20
>=20
> Ok, here is a question for all you STL users out there.
>=20
> 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).
>=20
> 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.
=20
> 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;
=20
// 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=3D0; i<E.size(); ++i)
{
for(vector<unsigned>::size_type j=3D0; j<A[i].size(); ++j)
{
al[ A[i][j] ]=3D something;
L.push_back(al[ A[i][j] ]);
}
}
sort(L.begin(), L.end());
system("cls");
=20
=20
for(map<unsigned, double>::size_type i=3D1; i<=3Dal.size(); ++i)
{
cout<<i<<": "<<al[i]<<"\t\t\t";
=20
cout<<L[i-1]<<": ";
=20
for(map<unsigned, double>::size_type j=3D1; j<=3Dal.size(); ++j)
{
if(L[i-1]=3D=3Dal[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.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|