473,385 Members | 1,409 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,385 software developers and data experts.

Using copy for output

Hi all,

Given a list of int, it's rather easy to output them line by line:
std::copy(mylist.begin(), mylist.end(),
std::ostream_iterator<int>(cout, "\n");

Now, there's two ways in which I would like to generalize this:
First, I would like to be able to print a list of pointers to int.
However, I'm guessing this is not possible with copy, right?
I would have to:
for(std::list<int>::const_iterator it = mylist.begin(); it !=
mylist.end() ; ++it) cout << **it << "\n";

Right? Not possible with copy?
Another thing I would like to do it to had a character in-between each
printed int. For example:
If I have a list with 1,2,3,4,5 and I want to print " + " I would get 1
+ 2 + 3 + 4 + 5.
A solution is:
std::string str(" + ");
for(std::list<int>::const_iterator it = mylist.begin(); it !=
mylist.end() ; ++it) {
cout << **it;
std::list<int>::const_iterator aux = it;
if(++aux != mylist.end())
cout << str;
}

Is there any better way? Comments on this? Anyway to do this more
straightforwardly?

Regards,

Paulo Matos

Dec 6 '06 #1
9 2418
Paulo Matos wrote:
If I have a list with 1,2,3,4,5 and I want to print " + " I would get 1
+ 2 + 3 + 4 + 5.
std::copy(mylist.begin(), mylist.end(),
std::ostream_iterator<int>(cout, " + ");

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 6 '06 #2

Pete Becker escreveu:
Paulo Matos wrote:
If I have a list with 1,2,3,4,5 and I want to print " + " I would get 1
+ 2 + 3 + 4 + 5.

std::copy(mylist.begin(), mylist.end(),
std::ostream_iterator<int>(cout, " + ");
That's what's I did iintially with '\n' however, it's not the same
thing. The result of that is:
1 + 2 + 3 + 4 + 5 +

And I don't want the extra plus in the end. What about if the list
contains pointers to ints?
--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 6 '06 #3
On 6 Dec 2006 13:05:01 -0800, "Paulo Matos" wrote:
>Pete Becker escreveu:
>Paulo Matos wrote:
If I have a list with 1,2,3,4,5 and I want to print " + " I would get 1
+ 2 + 3 + 4 + 5.

std::copy(mylist.begin(), mylist.end(),
std::ostream_iterator<int>(cout, " + ");

That's what's I did iintially with '\n' however, it's not the same
thing. The result of that is:
1 + 2 + 3 + 4 + 5 +

And I don't want the extra plus in the end.
add:

cout << "\b\b\b"; // works for some ostreams
>What about if the list contains pointers to ints?
I'd use a for loop anyway. copy is 'semantically' wrong for output.

Best wishes,
Roland Pibinger

Dec 6 '06 #4

Paulo Matos wrote:
Hi all,

Given a list of int, it's rather easy to output them line by line:
std::copy(mylist.begin(), mylist.end(),
std::ostream_iterator<int>(cout, "\n");

Now, there's two ways in which I would like to generalize this:
First, I would like to be able to print a list of pointers to int.
However, I'm guessing this is not possible with copy, right?
I would have to:
for(std::list<int>::const_iterator it = mylist.begin(); it !=
mylist.end() ; ++it) cout << **it << "\n";

Right? Not possible with copy?
Another thing I would like to do it to had a character in-between each
printed int. For example:
If I have a list with 1,2,3,4,5 and I want to print " + " I would get 1
+ 2 + 3 + 4 + 5.
A solution is:
std::string str(" + ");
for(std::list<int>::const_iterator it = mylist.begin(); it !=
mylist.end() ; ++it) {
cout << **it;
std::list<int>::const_iterator aux = it;
if(++aux != mylist.end())
cout << str;
}

Is there any better way? Comments on this? Anyway to do this more
straightforwardly?

Regards,

Paulo Matos
With an operator<< overload...

#include <iostream>
#include <ostream>
#include <list>
#include <algorithm>
#include <iterator>

template< typename T >
class NSequence
{
T t;
public:
NSequence(const T& r_t) : t(r_t) { }
T operator() () { return t++; }
};

template< typename T >
std::ostream&
operator<<(std::ostream& os, std::list< T >& r_l)
{
std::copy( r_l.begin(),
--r_l.end(),
std::ostream_iterator< int >(os, " + "));
return os << *(--r_l.end());
}

int main()
{
std::list< int nlist;
// insert integers from 1 to 6
std::generate_n( std::back_inserter(nlist),
6,
NSequence< int >(1) );
// print ints with " + "
std::cout << nlist << std::endl;
}

/*
1 + 2 + 3 + 4 + 5 + 6
*/

If you need the same with a std::list< int* >,
i'ld suggest wrapping it in a templated class with a std::list member.

Dec 6 '06 #5
Paulo Matos wrote:
Pete Becker escreveu:
>Paulo Matos wrote:
>>If I have a list with 1,2,3,4,5 and I want to print " + " I would get 1
+ 2 + 3 + 4 + 5.
std::copy(mylist.begin(), mylist.end(),
std::ostream_iterator<int>(cout, " + ");

That's what's I did iintially with '\n' however, it's not the same
thing. The result of that is:
1 + 2 + 3 + 4 + 5 +
Whoops, sorry. As long as you've got a bidirectional iterator, you can
do this:

mylist::iterator end = mylist.end();
if (mylist.begin() != end)
{
--end;
std::copy(mylist.begin(), end,
std::ostream_iterator<int>(cout, " + "));
std::copy(--mylist.end(), end,
std::ostream_iterator<int>(cout, ""));
}

Alternatively, you can write an algorithm that takes an output stream
rather than an iterator:

template <class InIt>
void show(InIt first, ostream& str, const char *sep)
{
if (first != last)
str << *first++;
while (first != last)
str << sep << *first++;
}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 7 '06 #6
Salt_Peter wrote:
return os << *(--r_l.end());
A note of caution here: this works if operator-- is a member of the
iterator type returned by end(), but doesn't work if it's a free
function. The problem is that -- modifies its argument. The call to
end() creates a temporary object, and you can't pass that to a
non-member function that takes its argument by reference.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 7 '06 #7

Pete Becker wrote:
Salt_Peter wrote:
return os << *(--r_l.end());

A note of caution here: this works if operator-- is a member of the
iterator type returned by end(), but doesn't work if it's a free
function. The problem is that -- modifies its argument. The call to
end() creates a temporary object, and you can't pass that to a
non-member function that takes its argument by reference.
Its a std::list so thats a bidirectional iterator with --iter and
iter-- member-operators available.
If the iterator involved was only a forward iterator, that would indeed
fail.

The warning about the temporary is relevant and for the record, i don't
like the above statement at all even if the reference to std::list<>
had been made const.

Thanks for your input.

Dec 7 '06 #8
Salt_Peter wrote:
Pete Becker wrote:
>Salt_Peter wrote:
>> return os << *(--r_l.end());
A note of caution here: this works if operator-- is a member of the
iterator type returned by end(), but doesn't work if it's a free
function. The problem is that -- modifies its argument. The call to
end() creates a temporary object, and you can't pass that to a
non-member function that takes its argument by reference.

Its a std::list so thats a bidirectional iterator with --iter and
iter-- member-operators available.
It's a std::list, so it provides a bidirectional iterator. The
requirement for bidirectional iterators is that the expressions --iter
and iter-- are both valid and have specified semantics. They are not
required to be implemented with member functions. If pre-increment is a
free function, --r_l.end() is ill-formed.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 7 '06 #9


Thanks a lot for all your input. :) Great, I just learned some more C++
today!

Dec 7 '06 #10

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

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
1
by: Hyunchan Kim | last post by:
To indent xml file, I made an instance of Transformer from Templates using following. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: Harrie | last post by:
Hi group, I want to indent existing XML files so they are more readable (at least to me). At this moment I'm looking at the XML files OpenOffice.org's Writer application produces in it's zipped...
1
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two...
2
by: Soddy | last post by:
Hello! I'm playing with an Access 2003 (Converted Northwind & Split) DB. I 'copy & paste' the split Access DB into the 'Solution' of C#.NET and make the 'connection'. I then 'copy & Paste' the...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
1
by: RolfK | last post by:
Hello Experts, I have a small problem with copy of CDATA sections. (I'm using XSLT2.0 ) My output target is defined as txt. In my xml source is a CDATA section to be put as it is into the...
3
by: WideBoy | last post by:
Hi, I have a software generated schema which creates a few empty elements, e.g. <xsd:sequence/>. I would like to be able to delete these elements from the schema post-generation using XSLT. ...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.