473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do you define key_compare and value_compare

I'm writing a restricted map<Str,Int> for an assignment, without using
the map template. One of the things I'm having trouble with is trying
to define the key_compare and value_compare for this class. How do you
define those?

Dennis

Jan 28 '06 #1
6 6134
Update: Okay, my assignment doesn't need it any more, but I'd still
like to learn about how to do this for future templates I might write.
Thanx.

Dennis

Jan 28 '06 #2
Dennis wrote:
I'm writing a restricted map<Str,Int> for an assignment, without using
the map template. One of the things I'm having trouble with is trying
to define the key_compare and value_compare for this class. How do you
define those?


I am not sure, I know what you mean. Within your template you should have
some typedefs:

template< typename KeyType, typename MappedType >
class map {

typedef KeyType const key_type;
typedef MappedType mapped_type;
typedef std::pair< key_type, mapped_type > value_type;

};

Then you will use std::less< key_type > and std::less< value_type > as the
default comparison functions.

If that is not what you meant, you will need to say more about the Str class
and the Int class.
Best

Kai-Uwe Bux
Jan 28 '06 #3
What I really meant to say was it is like a map<string,int> rather than
any special class I wrote myself. The description of key_compare is a
"Function object that compares two keys for ordering". So does that
mean it's a class within the map<string,int> that key_comp() uses?

Dennis

Jan 29 '06 #4
Dennis wrote:
What I really meant to say was it is like a map<string,int> rather than
any special class I wrote myself. The description of key_compare is a
"Function object that compares two keys for ordering". So does that
mean it's a class within the map<string,int> that key_comp() uses?


Not quite. In order to illustrate the significance of key_comp, let me take
a little detour and recall the way a map is typically implemented. Usually,
std::map< key_type, mapped_type > uses a (balanced) tree whose nodes store
pairs

std::pair< key_type const, mapped_type >

In order to find efficiently a node for a given key, the tree is organized
as a search tree, i.e., for any given node V, the left subtree has keys
less than the key at V and the right subtree only hosts keys not smaller
than the key at V.

Now, in order to compare keys, the map container uses a comparison function.
This function defaults to std::less<key_type>. You can, however, specify a
comparison function of your own choice. In order to do so, you pass a
function object (in the simplest case, a free standing function) as a
parameter upon construction of the map object.

In your example, map<string,int>: the default std::less<string> will be
lexicographic comparison of strings. If you know that your strings will
typically have long common initial segments but differ in length, you may
want to replace that with a short_lex order comparison predicate for
efficiency.
Hope this helps

Kai-Uwe Bux

Jan 29 '06 #5
I think that gets closer to my question. So the "short_lex" in your
explanation is like the new key_compare? If so, how would you write the
comparison predicate?

Feb 2 '06 #6
Dennis wrote:
I think that gets closer to my question. So the "short_lex" in your
explanation is like the new key_compare? If so, how would you write the
comparison predicate?


Please quote what you are referring to: (a) how am I supposed to remember
something that is in the past? and (b) how is anybode else supposed to make
sense of your question?

Anyway, the short_lex order for strings goes like so:

#include <string>
#include <algorithm>

bool short_lex_is_less ( std::string const & a,
std::string const & b ) {
if ( a.length() < b.length() ) {
return ( true );
}
if ( b.length() < a.length() ) {
return ( false );
}
// length did not decide:
return ( std::lexicographical_compare( a.begin(), a.end(),
b.begin(), b.end() ) );
}
But to use it with a map, you would need to do something like this:

typedef bool (*string_compare)( std::string const &, std::string const & );

#include <map>

int main ( void ) {
std::map< std::string, int, string_compare >
the_map ( short_lex_is_less );
}

Hope this helps

Kai-Uwe Bux
Feb 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
2
by: Andreas | last post by:
Hi! I'm using an IplImage library from camellia.sourceforge.net, and the testbench calls a file only containing code like the one below. In cam_morphomaths_code.c the real computation is made,...
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
34
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. ...
17
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.