473,786 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2220
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_t ype>
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_itera tor : public
std::iterator<s td::input_itera tor_tag,
typename std::iterator_t raits<It>::valu e_type::second_ type>
{
public:

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

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

bool operator==(cons t map_value_itera tor<It&mvi) const
{ return it == mvi.it; }
bool operator!=(cons t map_value_itera tor<It&mvi) const
{ return it != mvi.it; }
bool operator==(cons t It &i) const { return it == i; }
bool operator!=(cons t It &i) const { return it != i; }

const typename std::iterator_t raits<It>::valu e_type::second_ type *
operator->() const { return &it->second; }
const typename std::iterator_t raits<It>::valu e_type::second_ type &
operator*() const { return it->second; }

private:

It it;
};

You could then use it like this:

typedef std::map<int,in tIntMap;
typedef std::vector<int IntVec;

IntMap m;
IntVec v;

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

map_value_itera tor<IntMap::ite ratorbegin = m.begin();
map_value_itera tor<IntMap::ite ratorend = m.end();

std::partial_su m(begin,end,bac k_inserter(v));

Sep 11 '06 #2

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

Similar topics

1
2938
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... How can this be accomplished with CSS? Thanks,
1
2293
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 spaces. Also, I have tried things like this, but can't get them to work: <%# DataBinder.Eval(Container.DataItem, "lname") + ((DataBinder.Eval(Container.DataItem, "some_field")=="")?"":", ") + DataBinder.Eval(Container.DataItem, "fname")%>
6
4159
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
2789
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"> <HeaderStyle Width="30%" CssClass="folderHeader"></HeaderStyle> <ItemTemplate>
4
1692
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 I want to use the toString() method on them all. Is there a way of defining toString() so that I don't have to loop through all of the particles printing each individually. The code might explain it better than I can so the relevant part of...
2
1660
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 as compared to enclosing each question and answer within divs ? Perhaps , through JavaScript. Thanks in advance ..
6
2080
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 DeviceManager which is only interested in CD/DVD devices. I want to get the list of CD/DVD devices and "be informed when a disc inserted into a device". I am developing this on Linux. So, I used HAL API and read some system (actually /proc) files to gather...
1
1551
by: guile | last post by:
Hi. Here is the structure of my document Container --Header --Navigation --Main ----Sidebar ----Main Content --Footer Container Ends
6
2002
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 error on them now. The error occurs when I attempt a series of SQL statements wrapped in a TransactionScope(). It's executing against a different server, so this is where it's elevated to a distributed transaction.
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.