STL find & compare

T

Tom

Hi,
I need some help with stl maps.

I am using a std::map that is sorted in a very special way.
Therefore I am using my own sort function. That works fine.
But this is only half the problem. Additionally "map.find()" should
use a different comparison function.

My map is like:
map < myString, Whatever>
As far as I know "find" uses operators from the key (myString).
Therefore I wrote my own operator== ,operator< and operator>
Nothing worked. Does anybody know which comparison
function uses "find" or know any other solution while
using std functionality ?

thanks a lot T.
 
C

c wood

Tom said:
Hi,
I need some help with stl maps.

I am using a std::map that is sorted in a very special way.
Therefore I am using my own sort function. That works fine.
But this is only half the problem. Additionally "map.find()" should
use a different comparison function.

My map is like:
map < myString, Whatever>
As far as I know "find" uses operators from the key (myString).
Therefore I wrote my own operator== ,operator< and operator>
Nothing worked. Does anybody know which comparison
function uses "find" or know any other solution while
using std functionality ?

Maintain another index, sorted by the "real" value you want to find by.

By index I mean map. I usually put my elements in a set, ordered by pri
key, and add a map<2nd key, item *> index. Otherwise you'll end up
iterating through them all looking for a certain value.

Of course this all depends on the details of exactly what you are doing
too. If you elaborate a little more into the details, perhaps we can help
you better.
 
I

Ivan Vecerina

| I am using a std::map that is sorted in a very special way.
| Therefore I am using my own sort function. That works fine.
| But this is only half the problem. Additionally "map.find()" should
| use a different comparison function.
|
| My map is like:
| map < myString, Whatever>
| As far as I know "find" uses operators from the key (myString).
| Therefore I wrote my own operator== ,operator< and operator>
| Nothing worked. Does anybody know which comparison
| function uses "find" or know any other solution while
| using std functionality ?

map.find() uses the comparison function that is used for sorting
( an item that comes neither before nor after is considered equal).
This allows the search to be performed in O(logN) time using
the map's tree structure.

If you want to use another equality criterion, you can use
the std::find function:
std::find( map.begin(), map.end(), MyPredicate(someKey) );
Note that this search will take linear O(N) time.
The predicate object is very much like a sort function:
its operator() shall take a MyMap::value_type& as a reference,
and return true if the item matches your search criterion.

hth -Ivan
 
G

Gianni Mariani

Tom said:
Hi,
I need some help with stl maps.

I am using a std::map that is sorted in a very special way.
Therefore I am using my own sort function. That works fine.
But this is only half the problem. Additionally "map.find()" should
use a different comparison function.

My map is like:
map < myString, Whatever>
As far as I know "find" uses operators from the key (myString).
Therefore I wrote my own operator== ,operator< and operator>
Nothing worked. Does anybody know which comparison
function uses "find" or know any other solution while
using std functionality ?

There is an optional compare template parameter to std::map - see.

http://www.sgi.com/tech/stl/Map.html
 
T

Tom

I am not quite sure if nesting maps will really helps in my case.
I am using my own sort function, which works fine.

What I need is a find function, that returns equity if a character in the
key
is a dot "." (Nr. 46 in ASCII table). And it should return the first entry
that fits.

A simplified example would be:

Keys in Map are (in the correct order)
"PEBKACyesitisme382"
"PEBKAC....382"

if my search-string is "PEBCAKyesitisme382" & it should find
"PEBKACyesitisme382"

if the order is v.v.
"PEBKAC....382"
"PEBKACyesitisme382"

it is should find "PEBKAC....382" for "PEBCAKyesitisme382"

it would be great if any of You knows a generic solution using stl
implements.

Thanks a lot
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top