Hi,
I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't
work:
bool pointCompare(POINT p1, POINT p2) {
if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) )
return true;
return false;
}
int main() {
std::map<POINT,bool,pointCompare> myMap;
return 0;
}
That code won't compile, because pointCompare is not a valid
comparison operator. Below is code that does work -- it creates a
class and then defines the function operator on it. I'm unclear as to
why that method works while the one above does not.
Also, in the line:
bool operator() (const string& s1, const string& s2) const {
what is the final "const" doing there?
Thanks for any clarification,
cpp
-----WORKING CODE FOLLOWS-----
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
/* function object to compare strings
* - allows you to set the comparison criterion at runtime
* - allows you to compare case insensitive
*/
class RuntimeStringCmp {
public:
// constants for the comparison criterion
enum cmp_mode {normal, nocase};
private:
// actual comparison mode
const cmp_mode mode;
// auxiliary function to compare case insensitive
static bool nocase_compare (char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
public:
// constructor: initializes the comparison criterion
RuntimeStringCmp (cmp_mode m=normal) : mode(m) {
}
// the comparison
bool operator() (const string& s1, const string& s2) const {
if (mode == normal) {
return s1<s2;
}
else {
return lexicographical_compare (s1.begin(), s1.end(),
s2.begin(), s2.end(),
nocase_compare);
}
}
};
/* container type:
* - map with
* - string keys
* - string values
* - the special comparison object type
*/
typedef map<string,string,RuntimeStringCmp> StringStringMap; 5 3395
cppaddict wrote: Hi,
I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't work:
bool pointCompare(POINT p1, POINT p2) { if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) ) return true; return false; }
int main() { std::map<POINT,bool,pointCompare> myMap; return 0;
Hi, you could just define the < for your class POINT ( you better call
it Point ) and declare the map:
std::map <Point> myMap;
class Point
{
//....
bool operator < ( Point p2 )
{
return (y > p2.y || (y == p2.y && x > p2.x);
}
}
cppaddict wrote: Hi,
I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't work:
bool pointCompare(POINT p1, POINT p2) { if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) ) return true; return false; }
int main() { std::map<POINT,bool,pointCompare> myMap; return 0; }
That code won't compile, because pointCompare is not a valid comparison operator. Below is code that does work -- it creates a class and then defines the function operator on it. I'm unclear as to why that method works while the one above does not.
The map expects a type as 3rd template parameter, not a pointer to
function.
Also, in the line:
bool operator() (const string& s1, const string& s2) const {
what is the final "const" doing there?
It says that the operator won't change the function object.
Btw, you mustn't change the comparison function during the life time of
a map. The map expects a type as 3rd template parameter, not a pointer to function.
So what is the minimum implementation you'd have to give the function
object? Would it be:
class RuntimeStringCmp {
// the comparison
bool operator() (const string& s1, const string& s2) const {
if (mode == normal) {
return s1<s2;
}
else {
return lexicographical_compare (s1.begin(), s1.end(),
s2.begin(), s2.end(),
nocase_compare);
}
}
};
Thanks,
cpp
cppaddict wrote in news:vh********************************@4ax.com in
comp.lang.c++: I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't work:
bool pointCompare(POINT p1, POINT p2) { if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) ) return true; return false; }
int main() { std::map<POINT,bool,pointCompare> myMap; return 0; }
That code won't compile, because pointCompare is not a valid comparison operator.
The Compare paramiter to std::map<> is a type, this needs to be a functor
or a function-pointer type, *not* a function.
Example that works, note the typedef comp_t and the arguments passed to
map's constructor:
#include <iostream>
#include <map>
bool fless( int const &left, int const &right )
{
return left < right;
}
bool fgreater( int const &left, int const &right )
{
return right < left;
}
typedef bool (*comp_t)( int const &, int const & );
int main()
{
using namespace std;
map< int, int, comp_t > lmap( fless ), gmap( fgreater );
for ( int i = 0; i < 10; ++i )
{
lmap[i] = gmap[i] = i * i;
}
map< int, int, comp_t >::iterator ptr, lim;
cout << "less:\n";
for ( ptr = lmap.begin(), lim = lmap.end(); ptr != lim; ++ptr )
{
cout << ptr->first << " - " << ptr->second << "\n";
}
cout << "\ngreater:\n";
for ( ptr = gmap.begin(), lim = gmap.end(); ptr != lim; ++ptr )
{
cout << ptr->first << " - " << ptr->second << "\n";
}
}
HTH.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
cppaddict wrote: ... I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't work:
bool pointCompare(POINT p1, POINT p2) { if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) ) return true; return false; }
int main() { std::map<POINT,bool,pointCompare> myMap; return 0; }
That code won't compile, because pointCompare is not a valid comparison operator.
If you want to use a function pointer as a custom comparator for
'std::map', you have to declare and initialize your map as follows
std::map<POINT, bool, bool(*)(POINT, POINT)> myMap(pointCompare);
The third template parameter is the _type_ of the comparator, not the
actual instance. If you need to pass an instance of comparator to
'std::map', you have to pass it through constructor parameters.
--
Best regards,
Andrey Tarasevich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christopher |
last post by:
I am having problems iterating through the sequence of objects stored in my
std::map I want to call a function from the "value" object in the map. Here
is my snippet:
typedef std::map<SOCKET,...
|
by: Flzw |
last post by:
Well I have a map like this :
std::map <string, CObject> ObjectList;
I have a function like this :
CObject* NewObject( char* Name, CArg* Arg)
{
std::string key = Name;
ObjectList =...
|
by: Bob |
last post by:
Hi,
I'm trying to use a map with a string key, and a pointer to objects
contained in a vector. I've wrapped this in a class like so:
// cMap
template<class T> class cMap : public cList<T> { ...
|
by: Serengeti |
last post by:
Hello,
in my class I have a map that translates strings to pointers to some
member functions. The code goes like this:
class F {
typedef void (Function::*MathFuncPtr)();
std::map<std::string,...
|
by: Saeed Amrollahi |
last post by:
Dear All C++ Programmers
Hello
I am Saeed Amrollahi. I am a software engineer in
Tehran Sewerage Company. I try to use std::map and map::find member
function. I
use Visual Studio .NET. my...
|
by: vertigo |
last post by:
Hello
I have std::map object and i want to have randomly sorted objects in it.
I tried to:
std::map<int,RandomCompare> myobject;
and:
struct RandomCompare{
bool operator(int i1, int i2){
|
by: Dan Trowbridge |
last post by:
Hi everyone,
In my attempt to port code from VS 6.0 to VS.NET I had some code break along
the way, mostly due to not adhereing closely to the C++ standard. This may
be another instance but I...
|
by: kamaraj80 |
last post by:
Hi
I am using the std:: map as following.
typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
}SeatRowCols;
typedef struct _NetData
|
by: massysett |
last post by:
I'm puzzled about part of the standard. 23.1 states that items stored
in a container must be assignable. Therefore, the items in a map--that
is, std::pair<const Key, valuemust be assignable....
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |