|
From: Ioannis V. <iv...@em...> - 2003-06-30 19:18:29
|
One thing to not be confused in my example. The unsigned keys i am storing are sequential, that is 1,2,3,... that's why the map<unsigned,double>::size_type is used from 1 to <=.size(). I could have used an unsigned counter too. I am telling this so as to avoid possible newbie confusion, there is no separate access through a size_type. size_types are unsigned integer values used to describe the size (at least for usual containers, one can always define his own size_type and size() to describe what he imagines as "size" in his own classes. :-) ). Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of David McKen > Sent: Monday, June 30, 2003 8:53 PM > To: Dev-C Mailing List > Subject: RE: [Dev-C++] Reverse Map lookup. > > > 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 > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_06 1203_01/01 _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |