S
Saeed Amrollahi
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
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