Std::copy to std::cout for std::pair?

There is no standard way to cout a std::pair because, well, how you want it printed is probably different from the way the next guy wants it. This is a good use case for a custom functor or a lambda function. You can then pass that as an argument to std::for_each to do the work typedef std::map MyMap; template struct PrintMyMap : public std::unary_function { std::ostream& os; PrintMyMap(std::ostream& strm) : os(strm) {} void operator()(const T& elem) const { os (std::cout)).

There is no standard way to cout a std::pair because, well, how you want it printed is probably different from the way the next guy wants it. This is a good use case for a custom functor or a lambda function. You can then pass that as an argument to std::for_each to do the work.

Typedef std::map MyMap; template struct PrintMyMap : public std::unary_function { std::ostream& os; PrintMyMap(std::ostream& strm) : os(strm) {} void operator()(const T& elem) const { os (std::cout)).

I'd just like to point out that adding things to the std:: namespace is illegal according to the C++ Standard (see section 17.4.3.1).

Important remark, thank you. – bayda Mar 11 '09 at 11:32 One exception: you may add overloads for std::swap. – Konrad Rudolph Mar 11 '09 at 12:29 @konrad go you have a reference for that?

– anon Mar 11 '09 at 12:37 @konrad s/go/do/ – anon Mar 11 '09 at 12:37 Herb Sutter, in one of his Gurus of the Week, suggested "using std::swap" at the start of each file, so that standard swaps and user-defined swaps would use the same syntax. – David Thornley Mar 11 '09 at 14:30.

What you want is a transforming iterator. This kind of iterator wraps another iterator, forwards all positioning methods like operator++ and operator==, but redefines operator* and operator->. Quick sketch : template struct transformingIterator : private ITER { transformingIterator(ITER const& base) : ITER(base) {} transformingIterator& operator++() { ITER::operator++(); return *this; } std::string operator*() const { ITER::value_type const& v = ITER::operator*(); return "" + v->first +", " + v->second + ""; } ...

Thanks. Nice idea to create iterator wrappers, this idea could be generalized and used for solve other problems. – bayda Mar 11 '09 at 13:12 If you want something generic, one obvious step is to store the transformation in an appropriate boost::function.

You'd need an extra template parameter for the new value_type, of course. – MSalters Mar 11 '09 at 15:37.

Using Boost Lambda, you could try something like this. The version I have of Boost Lambda, this doesn't actually work, I'll test and fix later. #include #include using namespace boost::lambda; std::for_each( some_map.begin(), some_map.end(), std::cout ::value_type::first, _1 ).

I'd rather delete this answer, but I'll leave it for now, in case someone finds the discussion interesting. Since it's a reasonable extension to the std library, I'd just put it in std namespace, especially if this is a one time thing. You can just declare it static to prevent it from causing linker errors, should someone else do the same thing someplace else.

Another solution that comes to mind is to create a wrapper for std::pair: template struct pairWrapper { const std::pair & x; pairWrapper(const std::pair & x) : x(x) {} } template std::ostream & operator.

1, very nice -- the converting constructor will automatically convert a pair to a pairWrapper when needed. But please add the ostream& parameter to your templated operator– bayda Mar 11 '09 at 11:31 Wrong, disallowed. Namespace std is for compiler-provided classes, templates and functions.

You may not add overloads. – MSalters Mar 11 '09 at 12:19 You'll probably need operator= for your pairWrapper as well because std::ostream_iterator requiers it and a compiler wouldn't be able to generate it for you due to a const member. – Serge Mar 11 '09 at 12:36 @Serge: Are you sure?

Because that would mean it can't print immutable objects at all. Maybe you mean the copy constructor? – TrayMan Mar 11 '09 at 13:03.

I've founded one new elegant way to solve this problem. I've got many interest ideas when read answers: wrap iterator, for transform std::pair to std::string; wrap std::pair, for have a chance to overload operator::first and std::pair::second members; I think I will use all of this ideas in future for solve different other problems. But for this case I've understaded that I can formulate my bproblem as "transform map's data to strings and write them to output stream" instead "copy map's data to ouput stream".

My solution looks like: namespace { std::string toString( const std::pair& data) { std::ostringstream str; str ( std::cout, "\n" ), toString ); I think this method is most short and expressive than others.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions