473,672 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using Functor for map values?

Hi,
I have the following function and functor and i want to print all
values of a map
but pair does not support operator << of course.
so i wonder what are my options?
can i extend the pair struct? can i create a values iterator?
how do i do it?

Thanks in advance.

template <class Iterator, class Function>
void apply(Iterator begin, Iterator end, const Function& f)
{
for(;begin != end; ++begin)
f(*begin);
}

class Print
{
public:
template<class Tvoid operator()(cons t T& printable) const
{
cout << printable << endl;
}
};

Feb 1 '07 #1
6 1836
On Feb 1, 10:38 am, tome...@gmail.c om wrote:
Hi,
I have the following function and functor and i want to print all
values of a map
but pair does not support operator << of course.
so i wonder what are my options?
can i extend the pair struct? can i create a values iterator?
how do i do it?

Thanks in advance.

template <class Iterator, class Function>
void apply(Iterator begin, Iterator end, const Function& f)
{
for(;begin != end; ++begin)
f(*begin);

}

class Print
{
public:
template<class Tvoid operator()(cons t T& printable) const
{
cout << printable << endl;
}

};
Do you want to print the key-value pair of just the value? If you want
the second you could change the line 'f(*begin);' to 'f(begin-
>second);', this would however make the function a lot less general. A
better approach, which works regardless if you want to print just the
value or the key-value pair, is to create an overloaded version of the
()-operator.

class Print
{
public:
template<class T>
void operator()(cons t T& printable) const
{
cout << printable << endl;
}
template<class T, class U>
void operator()(cons t pair<T, U>& printable) const
{
cout << printable.first << ": " << printable.secon d << endl;
}
};

--
Erik Wikström

Feb 1 '07 #2
I want to print the value only and it must be a general functor.

Thanks.

Feb 1 '07 #3
On Feb 1, 11:38 am, tome...@gmail.c om wrote:
I want to print the value only and it must be a general functor.
First of all, try to quote relevant sections of the post you are
replying to.

No, either you can take the code I posted in the previous message and
modify it a bit so that it only prints the value. Or you can define a
<<-operator for std::pair:

template<class T, class U>
std::ostream& operator<<(std: :ostream& os, const std::pair<T, U>& p)
{
os << p.second;
return os;
}

--
Erik Wikström

Feb 1 '07 #4
Or you can define a
<<-operator for std::pair:

template<class T, class U>
std::ostream& operator<<(std: :ostream& os, const std::pair<T, U>& p)
{
os << p.second;
return os;

}
Wouldn't that fail due to ADL unless it is put into std namespace?

Feb 2 '07 #5
On Feb 2, 4:11 pm, "dasjotre" <dasjo...@googl email.comwrote:
Or you can define a
<<-operator for std::pair:
template<class T, class U>
std::ostream& operator<<(std: :ostream& os, const std::pair<T, U>& p)
{
os << p.second;
return os;
}

Wouldn't that fail due to ADL unless it is put into std namespace?
Hmm, don't know. It worked in my little test program but that was just
one small file with all the code in it, so there might be cases where
it won't work.

--
Erik Wikström

Feb 2 '07 #6
On 2 Feb 2007 07:11:45 -0800, "dasjotre" <da******@googl email.comwrote:
>Or you can define a
<<-operator for std::pair:

template<cla ss T, class U>
std::ostream & operator<<(std: :ostream& os, const std::pair<T, U>& p)
{
os << p.second;
return os;

}

Wouldn't that fail due to ADL unless it is put into std namespace?
No. ADL will _include_ any operator<<(std: :ostream&, const std::pair<T,U>& ) in
std in the list of candidate functions, but it will not _exclude_ the version
that is defined in your current scope.

-dr
Feb 2 '07 #7

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

Similar topics

6
5924
by: Gert Van den Eynde | last post by:
Hi all, I'm struggling a bit with Functors generated for an ABC. This is the functor code: class Functor{ public: virtual double operator(double x)=0 }
8
3499
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
3
2160
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can instantiate in the following ways PQueue<int> pq1; PQueue<int, Functor> pq2; // where Functor is a name of user-defined class I also added another constructor to accept a function pointer so that
7
3540
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the vector, which is passed to the function not by reference but by value. Now, at the end of this mail...
4
2901
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
40
3123
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false. My current approach is to have...
2
1689
by: Shawn McGrath | last post by:
Can you use the standard binders (std::bind2nd) on a functor taking two different parameter types? Here's my code now: template <typename T> struct compareValString : public std::binary_function <resources::ScopedType<T>, std::string, bool > { typedef typename resources::ScopedType<TST;
2
2285
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling ----nextSibling ---->0 | |
11
2278
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
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
8680
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...
0
7446
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5705
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
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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.