By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

STL find & compare

P: n/a
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.

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Tom" <Th************@epost.de> wrote in message
news:bq**********@news.mch.sbs.de...
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.



Jul 22 '05 #2

P: n/a
"Tom" <Th************@epost.de> wrote in message
news:bq**********@news.mch.sbs.de...
| 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
--
http://ivan.vecerina.com
Jul 22 '05 #3

P: n/a
Tom wrote:
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

Jul 22 '05 #4

P: n/a
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

"c wood" <re******@verizon.net> wrote in message
news:XY*******************@nwrddc02.gnilink.net...

"Tom" <Th************@epost.de> wrote in message
news:bq**********@news.mch.sbs.de...
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.




Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.