473,406 Members | 2,377 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,406 software developers and data experts.

Adding Map Entries

Hi,

I have some code which uses std::map to associate cartesian POINTs
with values. My current syntax for adding new (POINT,value) entries
to the map is clunky -- it takes three lines for each entry. There
must be a way to trim it down:

<code>
#include <map>
#include <iostream>

typedef struct tagPOINT { // pt
long x;
long y;
} POINT;

//map comparison fctn
struct PointCompare {
bool operator() (POINT p1, POINT p2) const {
if (p1.x == p2.x)
return p1.y < p2.y;
else
return p1.x < p2.x;
}
};
int main() {
std::map<POINT, int,PointCompare> testPoints;

//THIS CODE IS HERE IS THE PROBLEM
POINT pnt1 = {1,1};
std::pair<POINT, int> pair1(pnt1,123);
testPoints.insert(pair1);

POINT pnt2 = {1,2};
std::pair<POINT, int> pair2(pnt2,456);
testPoints.insert(pair2);

std::cout << "direct access {1,1} -> " << testPoints[pnt1] <<
std::endl;

std::map<POINT, int, PointCompare>::iterator iter;
for (iter = testPoints.begin(); iter != testPoints.end();
++iter) {
POINT iterPnt = iter->first;
std::cout << "iterator access: iter->first = {" <<
iterPnt.x << "," << iterPnt.y << "}" << std::endl;
std::cout << "iterator access: iter->second = " <<
(iter->second) << std::endl;
}

return 0;
}

</code>

Thanks for any help,
cpp
Jul 22 '05 #1
5 2681
You can make things a bit simpler by making POINTs real objects
and adding a constructor so you can stamp out POINT objects
like POINT p(1,2) instead of the mechanism you are using.

You dont have to explicitly create variables for POINT and pair,
you can do things like this:
testPoints.insert( std::pair<POINT,int>(POINT(1,2), 456) );

this creates a temporary POINT object with values 1, 2 and a pair
object with that POINT and they int value 456.

The std::pair<POINT,int> is a bit of a mouthful, I thought you
could just do std::pair and the compiler should deduce the template
arguments, however you can just create a typedef to use instead if
you don't want to type that stuff in over and over.

I've tinkered with your original code below

#include <std_disclaimers.h>

#include <map>
#include <iostream>
struct POINT { // pt
long x;
long y;
POINT(long xx, long yy)
:x(xx), y(yy){}
} ;

//map comparison fctn
struct PointCompare {
bool operator() (POINT p1, POINT p2) const {
if (p1.x == p2.x)
return p1.y < p2.y;
else
return p1.x < p2.x;
}
};
int main() {
std::map<POINT, int,PointCompare> testPoints;

POINT pnt2(1,2);

testPoints.insert( std::pair<POINT,int>(POINT(1,2), 456) );
POINT pnt1(1,2);
std::cout << "direct access {1,1} -> " << testPoints[pnt1] <<
std::endl;

std::map<POINT, int, PointCompare>::iterator iter;
for (iter = testPoints.begin(); iter != testPoints.end();
++iter) {
POINT iterPnt = iter->first;
std::cout << "iterator access: iter->first = {" <<
iterPnt.x << "," << iterPnt.y << "}" << std::endl;
std::cout << "iterator access: iter->second = " <<
(iter->second) << std::endl;
}

return 0;
}

"cppaddict" <he***@hello.com> wrote in message
news:m7********************************@4ax.com...
Hi,

I have some code which uses std::map to associate cartesian POINTs
with values. My current syntax for adding new (POINT,value) entries
to the map is clunky -- it takes three lines for each entry. There
must be a way to trim it down:

<code>
#include <map>
#include <iostream>

typedef struct tagPOINT { // pt
long x;
long y;
} POINT;

//map comparison fctn
struct PointCompare {
bool operator() (POINT p1, POINT p2) const {
if (p1.x == p2.x)
return p1.y < p2.y;
else
return p1.x < p2.x;
}
};
int main() {
std::map<POINT, int,PointCompare> testPoints;

//THIS CODE IS HERE IS THE PROBLEM
POINT pnt1 = {1,1};
std::pair<POINT, int> pair1(pnt1,123);
testPoints.insert(pair1);

POINT pnt2 = {1,2};
std::pair<POINT, int> pair2(pnt2,456);
testPoints.insert(pair2);

std::cout << "direct access {1,1} -> " << testPoints[pnt1] <<
std::endl;

std::map<POINT, int, PointCompare>::iterator iter;
for (iter = testPoints.begin(); iter != testPoints.end();
++iter) {
POINT iterPnt = iter->first;
std::cout << "iterator access: iter->first = {" <<
iterPnt.x << "," << iterPnt.y << "}" << std::endl;
std::cout << "iterator access: iter->second = " <<
(iter->second) << std::endl;
}

return 0;
}

</code>

Thanks for any help,
cpp

Jul 22 '05 #2
On Fri, 23 Jul 2004 16:53:58 -0700, cppaddict wrote:
Hi,

I have some code which uses std::map to associate cartesian POINTs with
values. My current syntax for adding new (POINT,value) entries to the
map is clunky -- it takes three lines for each entry. There must be a
way to trim it down:

<code>
#include <map>
#include <iostream>

typedef struct tagPOINT { // pt
long x;
long y;
} POINT;
You don't need the typedef trick for struct declarations in C++:

struct tagPOINT { /* ... */ };
or
struct POINT { /* ... */ };

It is convenient to define a constructor even for a simple struct so
that the code will be more readable (see below):

struct POINT
{
long x;
long y;

POINT(long x_arg, long y_arg)
:
x(x_arg),
y(y_arg)
{}
};

[...]
int main() {
std::map<POINT, int,PointCompare> testPoints;
You can use typedefs to simplify the code:

typedef std::map<POINT, int,PointCompare> Points;
typedef Points::iterator PointIter;

Points testPoints;

//THIS CODE IS HERE IS THE PROBLEM
POINT pnt1 = {1,1};
std::pair<POINT, int> pair1(pnt1,123);
testPoints.insert(pair1);
Now you can replace the above three lines with

testPoints.insert(std::make_pair(POINT(1, 1), 123));

Alternatively:

testPoints[POINT(1, 1)] = 123;

[...]
std::map<POINT, int, PointCompare>::iterator iter;
for (iter = testPoints.begin(); iter != testPoints.end(); ++iter)


The typedef defined above will make this simpler too:

for (PointIter iter = /* ... */)

Ali
Jul 22 '05 #3
On Fri, 23 Jul 2004 17:20:29 -0700, Dave Townsend wrote:
The std::pair<POINT,int> is a bit of a mouthful, I thought you could
just do std::pair and the compiler should deduce the template arguments,


Argument deduction works only for function templates; not class
templates.

Some template classes have a corresponding function template to do the
argument deduction. For example:

template <class A>
class Foo
{
public:
Foo(A const & a);
/* ... */
};

template <class A>
Foo<A> MakeFoo(A const & a)
{
return Foo<A>(a);
}

The convenience function for std::pair is std::make_pair.

Ali
Jul 22 '05 #4
Dave and Ali,

Thanks for the tips.

cpp
Jul 22 '05 #5
cppaddict <he***@hello.com> wrote in message news:<m7********************************@4ax.com>. ..
Hi,

I have some code which uses std::map to associate cartesian POINTs
with values. My current syntax for adding new (POINT,value) entries
to the map is clunky -- it takes three lines for each entry. There
must be a way to trim it down:


There is. Some other parts can be a bit neater as well:

#include <map>
#include <iostream>
#include <iterator>
#include <algorithm>

class POINT {
long x;
long y;

friend std::ostream &operator<<(std::ostream &os, POINT const &p) {
return os << "{" << p.x << "," << p.y << "}";
}
public:
bool operator<(POINT const &p2) const {
if (x == p2.x)
return y < p2.y;
else
return x < p2.x;
}

POINT(long x_init, long y_init) : x(x_init), y(y_init) {}
};

typedef std::pair<POINT, int> pp;

std::ostream &operator<<(std::ostream &os, pp const &p) {
return os << "point " << p.first << " -> " << p.second;
}

int main() {
std::map<POINT, int> testPoints;

testPoints[POINT(1,1)] = 123;
testPoints[POINT(1,2)] = 456;

std::cout << "point {1,1} -> " << testPoints[POINT(1,1)]
<< " (direct access)\n";
std::copy(testPoints.begin(), testPoints.end(),
std::ostream_iterator<pp>(std::cout, " (algorithm)\n"));

return 0;
}
Jul 22 '05 #6

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

Similar topics

0
by: Gary Lundquest | last post by:
I have an application with MS Access 2000 as the front end and MySQL as the back end. All was well until I upgraded the MySQL (Linux) server. The Problem: I insert data into a cumulative table....
20
by: Ash Phillips | last post by:
Hi Everyone, I have this program I wrote in VB6 for family use. It's a DVD Database just for me to keep track of them cause I have so many lol. In VB6, I could add items to the ListView in...
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
5
by: JJ | last post by:
I want to have two lists (may have to be listboxes) on a page that are populated from a database. I then need to be able to click on an entry in one box and add it to the other, _preferably_...
0
by: vljones | last post by:
Hoping to find a way of entering new data into a combo box which contains a list of items contained in a table. In addition I want to allocate an ID number to entries that are not currently in...
8
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi just wondering if anyone had any ideas on how to do this in C#. I am trying to get the total time of several start and stop time data entries. For example the input data looks like starttime...
1
by: jeff8676 | last post by:
I'm very new at Access, so if this is a lame question I'm sorry. Anyways, I'm trying to make a query that returns unique entries from a table, but when it finds two entries where everything...
9
by: =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post by:
``odict.byindex(index)`` For this API, I think it's important to make some performance guarantees. It seems fairly difficult to make byindex O(1), and simultaneously also make insertion/deletion...
4
by: Mikey C | last post by:
Hi Everyone, I understand in many cases, adding to history when updating window.location.hash is a GOOD thing. In my case, I want to update bookmarkability from javascript (post-load), but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
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...

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.