Re: Correct swapping
In article <1176934203.246655.276160@y80g2000hsf.googlegroups.com>,
Daniel Kr?gler <daniel.kruegler@googlemail.com> wrote:
Another point to consider is, that it's usually a bad idea, to
provide operator<< on some arbitrary std::pair, because neither
std::pair nor std::ostream are *your* types. To fix your problem
I recommend in this special situation to use one of the following:
(a) Replace your std::copy invocation by a std::for_each call
(which uses a special print functor and *not* operator<< on
std::pair) or an explicit loop.
(b) Use a better customizable iterator like boost::transform_iterator
which uses the in (a) mentioned print-functor. This ansatz is
nearly identical to your std::copy approach, where you simply
r
Depends if I have a complicated function using an output iterator
I would just create a small class and the needed operator << ().
struct Derived:std::pair<double,double>
{
Derived(double a,double b):std::pair<double,double>(a,b){}
Derived(std::pair<double,double> const
&a):std::pair<double,double>(a){};
Derived & operator == (const std::pair<double,double> &x)
{
first = x.first;
second = x.second
return *this;
};
};
inline std::ostream & operator << (std::istream &s,const Derived &x)
{
return s << '(' << x.first << ',' << x.second << ')';
}
now if I need operator << () I make sure I have a Derived and not an
std::pair<double,double> [it I miss it the compiler will complain, and
I can fix it.
std::vector<std::pair<double,double> > stuff;
// set up stuff
complicated_algorithm(stuff.begin(),stuff.end(),std::ostream_iterator<
Derived > (std::cout,"\n"));
std::ostream_iterator<T,C,Tr>::operator = (const T &) is not a template
function so conversion exists from the ctor taking a const pair<> & and
all is well.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]