473,509 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3538

"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
29244
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
17261
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
2744
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
3382
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
8721
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
6458
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
9633
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
2380
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
3355
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...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
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,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.