472,988 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Trouble in using std::map and its find member function

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 program uses two MFC classes: CRect and
CPoint
which represents Rectangle and Point concepts (as usual) and a user
defined class called GISCode which represents Global Information
System's code of Tehran city:
class GISCode
{
friend bool operator<(const GISCode&, const GISCode&);
public:
GISCode(const std::string&);
operator std::string() const; // get code
const std::string GetCode() const; // get code
// Equality Operations (generated)
bool operator==(const GISCode&) const;
bool operator!=(const GISCode&) const;
// Exception class(es)
class CodeError : public std::logic_error {
public:
CodeError(const std::string& = "");
const char* what() const;
private:
const std::string Code;
const std::string ErrMsg;
};
private:
void CheckCode(const std::string&) const;
std::string Code;
};

Tehran is a metropolitan city and contains 1080 of
these codes. I have to implement a program which shows
these codes on screen graphically in rectangles and by
each click on rectangle extracts GISCode
and finally shows a digging map of sewerage piping.
I use the following data structure:
map<CRect, GISCode> m;
I should implement a function object called
PointInRect:
// Function object for determining is a point in Rect?
class PointInRect {
public:
PointInRect(const CPoint pp) : p(pp) {}
bool operator()(const CRect& r) { return r.PtInRect(p) == TRUE ?
true : false;}
private:
const CPoint p;
};
at first I try to implement the following function:
void CTehranCityView::OnLButtonDblClk(UINT nFlags,
CPoint point)
{
map<CRect, GISCode> m = pDoc->GISRect;
// very naive implementation
find_if(m.begin(), m.end(),
PointInRect(...)); // fail!
I failed to use function object properly. My first
question is how to use such fair complex function
object in map?
After that I try to fill a vector with key elements of
map and then find CRect object in the vector using
function object:
void CTehranCityView::OnLButtonDblClk(UINT nFlags,
CPoint point)
{
vector<CRect> v;
for (map<CRect, GISCode>::const_iterator cit1 = m.begin(); cit1 !=
m.end(); cit1++) {
v.push_back(cit1->first);
}
vector<CRect>::const_iterator cit2 =
find_if(v.begin(), v.end(), PointInRect(point)); //(1)
if (cit2 != v.end()) {
map<CRect, GISCode>::const_iterator
cit3 = m.find(*cit2); //(2)
if (cit3 != m.end()) //(3)
GISCode c = cit3->second;
}
CFormView::OnLButtonDblClk(nFlags, point);
}

why the code at line (1) works?
Unfortunately at the line (2) cit3 == m.end() and I
can't extract the GISCode (at line (3)). The map is
filled by codes already.

how to solve this problem?

Regards,
Saeed Amrollahi
Jul 23 '05 #1
1 3491

"Saeed Amrollahi" <s_*********@yahoo.com> wrote in message
news:7f**************************@posting.google.c om...
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 program uses two MFC classes: CRect and
CPoint
which represents Rectangle and Point concepts (as usual) and a user
defined class called GISCode which represents Global Information
System's code of Tehran city:
class GISCode
{
friend bool operator<(const GISCode&, const GISCode&);
public:
GISCode(const std::string&);
operator std::string() const; // get code
const std::string GetCode() const; // get code
// Equality Operations (generated)
bool operator==(const GISCode&) const;
bool operator!=(const GISCode&) const;
// Exception class(es)
class CodeError : public std::logic_error {
public:
CodeError(const std::string& = "");
const char* what() const;
private:
const std::string Code;
const std::string ErrMsg;
};
private:
void CheckCode(const std::string&) const;
std::string Code;
};

Tehran is a metropolitan city and contains 1080 of
these codes. I have to implement a program which shows
these codes on screen graphically in rectangles and by
each click on rectangle extracts GISCode
and finally shows a digging map of sewerage piping.
I use the following data structure:
map<CRect, GISCode> m;
I should implement a function object called
PointInRect:
// Function object for determining is a point in Rect?
class PointInRect {
public:
PointInRect(const CPoint pp) : p(pp) {}
bool operator()(const CRect& r) { return r.PtInRect(p) == TRUE ?
true : false;}
private:
const CPoint p;
};
at first I try to implement the following function:
void CTehranCityView::OnLButtonDblClk(UINT nFlags,
CPoint point)
{
map<CRect, GISCode> m = pDoc->GISRect;
// very naive implementation
find_if(m.begin(), m.end(),
PointInRect(...)); // fail!
I failed to use function object properly. My first
question is how to use such fair complex function
object in map?
After that I try to fill a vector with key elements of
map and then find CRect object in the vector using
function object:
void CTehranCityView::OnLButtonDblClk(UINT nFlags,
CPoint point)
{
vector<CRect> v;
for (map<CRect, GISCode>::const_iterator cit1 = m.begin(); cit1 !=
m.end(); cit1++) {
v.push_back(cit1->first);
}
vector<CRect>::const_iterator cit2 =
find_if(v.begin(), v.end(), PointInRect(point)); //(1)
if (cit2 != v.end()) {
map<CRect, GISCode>::const_iterator
cit3 = m.find(*cit2); //(2)
if (cit3 != m.end()) //(3)
GISCode c = cit3->second;
}
CFormView::OnLButtonDblClk(nFlags, point);
}

why the code at line (1) works?
Unfortunately at the line (2) cit3 == m.end() and I
can't extract the GISCode (at line (3)). The map is
filled by codes already.

how to solve this problem?

MFC is not standard, thus not topical here, so I created
simple 'placeholder' classes for 'CPoint' and 'CRect' in
this example. I also did not check the coordinate
arithmetic thoroughly. You'll have to decide what
makes one 'CPoint' value less than another, the same for
'CRect'; I just made it up as I went along. :-)
I also reformatted things a bit to make it easier for
me to work with.

Anyway, this should get you started (and I suspect
others will jump in with corrections and ideas as well;
I'm not convinced this design is optimal for what
you're doing:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <ostream>
#include <string>

class CPoint
{
public:
friend bool operator<(const CPoint& lhs, const CPoint& rhs)
{
return (lhs.x < rhs.x) || (lhs.y < rhs.y);
}

friend bool operator >=(const CPoint& lhs, const CPoint& rhs)
{
return !(lhs < rhs);
}

friend std::ostream& operator<<(std::ostream& os, const CPoint& p)
{
return os << '('
<< std::setw(2) << p.x
<< ", "
<< std::setw(2) << p.y
<< ')';
}

CPoint(long xx = 0, long yy = 0) : x(xx), y(yy)
{
}

private:
long x;
long y;
};

class CRect
{
friend bool operator<(const CRect& lhs, const CRect& rhs)
{
return lhs.topleft < rhs.topleft;
}

public:
CRect(long x, long y, long width, long height)
: topleft(x, y), bottomright(x + width, y + height)
{
}

bool PtInRect(const CPoint& p) const
{
return p >= topleft && p < bottomright;
}

private:
CPoint topleft;
CPoint bottomright;
};

class GISCode
{
friend bool operator<(const GISCode&, const GISCode&);

public:
GISCode(const std::string& c = "") : Code(c)
{
}

operator std::string() const; // get code
const std::string GetCode() const
{
return Code;
}

// Equality Operations (generated)
bool operator==(const GISCode&) const;
bool operator!=(const GISCode&) const;

// Exception class(es)
class CodeError : public std::logic_error
{
public:
CodeError(const std::string& = "");
const char* what() const;
private:
const std::string Code;
const std::string ErrMsg;
};

private:
void CheckCode(const std::string&) const;
std::string Code;
};

// Function object for determining is a point in Rect?
class PointInRect
{
public:
PointInRect(const CPoint& pp) : p(pp) {}
bool operator()(const std::pair<CRect, GISCode>& r)
{
return r.first.PtInRect(p);
}

private:
const CPoint p;
};

class CTehranCityView
{
public:
CTehranCityView()
{
m[CRect( 5, 0, 15, 10)] = GISCode("ABC");
m[CRect(20, 0, 12, 10)] = GISCode("DEF");
m[CRect( 0, 10, 20, 15)] = GISCode("GHI");
}

void OnLButtonDblClk(unsigned int nFlags, const CPoint& point)
{
std::map<CRect, GISCode>::const_iterator it =
std::find_if(m.begin(), m.end(), PointInRect(point));

std::cout << "Point " << point << ": ";

std::cout << "Code ";

if(it == m.end())
std::cout << "not found\n";
else
{
std::cout << it->second.GetCode() << '\n';
}
}

private:
std::map<CRect, GISCode> m;
};

int main()
{
std::cout << "m[CRect( 5, 0, 15, 10)] = GISCode(\"ABC\");\n";
std::cout << "m[CRect(20, 0, 12, 10)] = GISCode(\"DEF\");\n";
std::cout << "m[CRect( 0, 10, 20, 15)] = GISCode(\"GHI\");\n";

CTehranCityView cv;
long x = 0;
long y = 0;

while(x < 36)
{
while(y < 36)
{
cv.OnLButtonDblClk(0, CPoint( x, y));
y += 5;
}

x += 5;
y = 0;
}

return 0;
}
Output:

m[CRect( 5, 0, 15, 10)] = GISCode("ABC");
m[CRect(20, 0, 12, 10)] = GISCode("DEF");
m[CRect( 0, 10, 20, 15)] = GISCode("GHI");
Point ( 0, 0): Code not found
Point ( 0, 5): Code not found
Point ( 0, 10): Code GHI
Point ( 0, 15): Code GHI
Point ( 0, 20): Code GHI
Point ( 0, 25): Code GHI
Point ( 0, 30): Code GHI
Point ( 0, 35): Code GHI
Point ( 5, 0): Code ABC
Point ( 5, 5): Code ABC
Point ( 5, 10): Code GHI
Point ( 5, 15): Code GHI
Point ( 5, 20): Code GHI
Point ( 5, 25): Code GHI
Point ( 5, 30): Code GHI
Point ( 5, 35): Code GHI
Point (10, 0): Code ABC
Point (10, 5): Code ABC
Point (10, 10): Code GHI
Point (10, 15): Code GHI
Point (10, 20): Code GHI
Point (10, 25): Code GHI
Point (10, 30): Code GHI
Point (10, 35): Code GHI
Point (15, 0): Code ABC
Point (15, 5): Code ABC
Point (15, 10): Code GHI
Point (15, 15): Code GHI
Point (15, 20): Code GHI
Point (15, 25): Code GHI
Point (15, 30): Code GHI
Point (15, 35): Code GHI
Point (20, 0): Code ABC
Point (20, 5): Code ABC
Point (20, 10): Code GHI
Point (20, 15): Code GHI
Point (20, 20): Code GHI
Point (20, 25): Code DEF
Point (20, 30): Code DEF
Point (20, 35): Code DEF
Point (25, 0): Code ABC
Point (25, 5): Code ABC
Point (25, 10): Code GHI
Point (25, 15): Code GHI
Point (25, 20): Code GHI
Point (25, 25): Code DEF
Point (25, 30): Code DEF
Point (25, 35): Code DEF
Point (30, 0): Code ABC
Point (30, 5): Code ABC
Point (30, 10): Code GHI
Point (30, 15): Code GHI
Point (30, 20): Code GHI
Point (30, 25): Code DEF
Point (30, 30): Code DEF
Point (30, 35): Code DEF
Point (35, 0): Code ABC
Point (35, 5): Code ABC
Point (35, 10): Code GHI
Point (35, 15): Code GHI
Point (35, 20): Code GHI
Point (35, 25): Code not found
Point (35, 30): Code not found
Point (35, 35): Code not found
-Mike
Jul 23 '05 #2

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

Similar topics

5
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,...
24
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
2
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,...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
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
2
by: mergaite_lietuvaite | last post by:
Hi, I try to use map. My code is very easy, but there are some errors.... (by compiling). Plaese, could you say, what I do wrong.. struct less_array { bool operator()(const FArray p1, const...
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.