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

applying partial_sum to a map container

Hi,

I have a map container where the key_type is a pointer to a class and
the value_type is an intenger. I'd like to apply the STL algorithm
partial_sum to the value_type field of that map but I don't know how to
do it. Probably I have to specify a functor as fourth argument of
partial_sum but I don't know how to implement that.
Can anyone help?

Thanks and regards
Cesco

Sep 11 '06 #1
1 2198
cesco wrote:
>
I have a map container where the key_type is a pointer to a class
If you use a pointer as a key type to an associateive container, you
need to make sure you provide a comparison that's based on the
referent of the pointer. Comparing pointers based on their actual
values is only meaningful when they are pointers into the same
object/array.
and the value_type is an intenger.
I think you mean data_type is an integer. value_type is actually a
std::pair<const key_type,data_type>
I'd like to apply the STL algorithm partial_sum to the value_type field of
that map but I don't know how to do it. Probably I have to specify a functor
as fourth argument of partial_sum but I don't know how to implement that.
I thought functor at first, too, but that won't work. The initial
value of the sum is set to the value at the beginning of
the input range, which, for a map, is a std::pair.

What you can do, though, is write a wrapper iterator that
returns only the second part of each pair:

#include <iterator>

template <typename It>
class map_value_iterator : public
std::iterator<std::input_iterator_tag,
typename std::iterator_traits<It>::value_type::second_type>
{
public:

map_value_iterator(It it) : it(it) {}
map_value_iterator(const map_value_iterator<It&mvi)
: it(mvi.it) {}

map_value_iterator &operator++() { ++it; return *this; }
map_value_iterator operator++(int) { It i = it; ++it; return i; }

bool operator==(const map_value_iterator<It&mvi) const
{ return it == mvi.it; }
bool operator!=(const map_value_iterator<It&mvi) const
{ return it != mvi.it; }
bool operator==(const It &i) const { return it == i; }
bool operator!=(const It &i) const { return it != i; }

const typename std::iterator_traits<It>::value_type::second_type *
operator->() const { return &it->second; }
const typename std::iterator_traits<It>::value_type::second_type &
operator*() const { return it->second; }

private:

It it;
};

You could then use it like this:

typedef std::map<int,intIntMap;
typedef std::vector<intIntVec;

IntMap m;
IntVec v;

m[0] = 1;
m[1] = 2;
m[2] = 4;

map_value_iterator<IntMap::iteratorbegin = m.begin();
map_value_iterator<IntMap::iteratorend = m.end();

std::partial_sum(begin,end,back_inserter(v));

Sep 11 '06 #2

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

Similar topics

1
by: Neil Zanella | last post by:
Hello, I would like to use CSS to apply a width of 100% to all <input> elements, but to only those that have an type attribute set to "text", without affecting check boxes, radio buttons, etc......
1
by: Charlie | last post by:
Hi: Is it possible to apply formatting to above statements which insert field values into HTML when binding to a datasource using Repeater control? For example, I would like to trim trailing...
6
by: Tim Meagher | last post by:
Can anyone help me figure out how to apply a stylesheet to a pushbutton defined in the asp:BoundColumn or asp:EditCommandColumn elements of a datagrid?
2
by: Liza | last post by:
Hi I have a problem gettting the right data in my CommandArgument after I applyed a rowfilter. My template column looks somewhat like this: <asp:TemplateColumn HeaderText="ErrorID">...
4
by: Cleverbum | last post by:
I have created a class Particle which has a method toString() that prints out all the useful information. In my main program I create an array of these objects and once I've fiddled with them a bit...
2
by: viveklinux | last post by:
Hi, Have a FAQ page , where have many sets of questions and answers. The questions need to be a different colour / style and the answers different. Was wondering is there a easy way to do this...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
1
by: guile | last post by:
Hi. Here is the structure of my document Container --Header --Navigation --Main ----Sidebar ----Main Content --Footer Container Ends
6
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
Hi - I have 4 webservers in my webfarm. All Win2k3 web edition. Before yesterday, none of them were service packed. I have now applied SP2 to two of them, and I'm getting a very weird MSDTC...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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
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.