Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13601 > unrolled thread
| Started by | Ttodir <sanmrtn96@gmail.com> |
|---|---|
| First post | 2012-01-26 18:12 -0800 |
| Last post | 2012-01-28 11:02 -0800 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.c++
Help with reading formatted input from an istream Ttodir <sanmrtn96@gmail.com> - 2012-01-26 18:12 -0800
Re: Help with reading formatted input from an istream Ian Collins <ian-news@hotmail.com> - 2012-01-27 20:21 +1300
Re: Help with reading formatted input from an istream Leigh Johnston <leigh@i42.co.uk> - 2012-01-27 12:47 +0000
Re: Help with reading formatted input from an istream Ttodir <sanmrtn96@gmail.com> - 2012-01-28 11:02 -0800
| From | Ttodir <sanmrtn96@gmail.com> |
|---|---|
| Date | 2012-01-26 18:12 -0800 |
| Subject | Help with reading formatted input from an istream |
| Message-ID | <48680e14-e47a-4778-931e-bc6ec6a40d87@t7g2000vbg.googlegroups.com> |
Consider the following program:
#include <iostream>
#include <iterator>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
typedef string T; // to simplify, always consider T as string
template<typename input_iterator>
void do_something(const input_iterator& first, const input_iterator&
last) {
const ostream_iterator<T> os(cout, "\n");
const set<T> words(first, last);
copy(words.begin(), words.end(), os);
}
int main(int argc, char** argv) {
const istream_iterator<T> is(cin), eof;
do_something(is, eof);
return 0;
}
The program extracts all the words from an istream (cin) and does
something with them. Each word is seperated by a white space by
default. The logic behind the formatted extraction is inside the
istream_iterator.
What I need to do now is to pass to do_something() two iterators so
that the extracted words will be separated by a punctuation character
instead of a white space (white spaces will be considered as "normal"
characters). How would you do that in a "clean C++ way" (that is, with
the minimum effort)?
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2012-01-27 20:21 +1300 |
| Message-ID | <9of1isF8j0U2@mid.individual.net> |
| In reply to | #13601 |
On 01/27/12 03:12 PM, Ttodir wrote:
> Consider the following program:
>
> #include<iostream>
> #include<iterator>
> #include<string>
> #include<set>
> #include<algorithm>
>
> using namespace std;
>
> typedef string T; // to simplify, always consider T as string
>
> template<typename input_iterator>
> void do_something(const input_iterator& first, const input_iterator&
> last) {
> const ostream_iterator<T> os(cout, "\n");
> const set<T> words(first, last);
> copy(words.begin(), words.end(), os);
> }
>
> int main(int argc, char** argv) {
> const istream_iterator<T> is(cin), eof;
> do_something(is, eof);
> return 0;
> }
>
> The program extracts all the words from an istream (cin) and does
> something with them. Each word is seperated by a white space by
> default. The logic behind the formatted extraction is inside the
> istream_iterator.
>
> What I need to do now is to pass to do_something() two iterators so
> that the extracted words will be separated by a punctuation character
> instead of a white space (white spaces will be considered as "normal"
> characters). How would you do that in a "clean C++ way" (that is, with
> the minimum effort)?
Derive your own input iterator class from std::input_iterator.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2012-01-27 12:47 +0000 |
| Message-ID | <A_2dnVenIrp0Ab_SnZ2dnUVZ7v2dnZ2d@giganews.com> |
| In reply to | #13601 |
On 27/01/2012 02:12, Ttodir wrote:
> Consider the following program:
>
> #include<iostream>
> #include<iterator>
> #include<string>
> #include<set>
> #include<algorithm>
>
> using namespace std;
>
> typedef string T; // to simplify, always consider T as string
>
> template<typename input_iterator>
> void do_something(const input_iterator& first, const input_iterator&
> last) {
> const ostream_iterator<T> os(cout, "\n");
> const set<T> words(first, last);
> copy(words.begin(), words.end(), os);
> }
>
> int main(int argc, char** argv) {
> const istream_iterator<T> is(cin), eof;
> do_something(is, eof);
> return 0;
> }
>
> The program extracts all the words from an istream (cin) and does
> something with them. Each word is seperated by a white space by
> default. The logic behind the formatted extraction is inside the
> istream_iterator.
>
> What I need to do now is to pass to do_something() two iterators so
> that the extracted words will be separated by a punctuation character
> instead of a white space (white spaces will be considered as "normal"
> characters). How would you do that in a "clean C++ way" (that is, with
> the minimum effort)?
The "clean C++ way" would be to imbue your own ctype facet which lets
you classify what exactly is white space.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Ttodir <sanmrtn96@gmail.com> |
|---|---|
| Date | 2012-01-28 11:02 -0800 |
| Message-ID | <e96f704e-13c9-482a-9037-f97a4cea3ec2@b23g2000yqn.googlegroups.com> |
| In reply to | #13607 |
On Jan 27, 12:47 pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 27/01/2012 02:12, Ttodir wrote:
>
>
>
>
>
>
>
>
>
> > Consider the following program:
>
> > #include<iostream>
> > #include<iterator>
> > #include<string>
> > #include<set>
> > #include<algorithm>
>
> > using namespace std;
>
> > typedef string T; // to simplify, always consider T as string
>
> > template<typename input_iterator>
> > void do_something(const input_iterator& first, const input_iterator&
> > last) {
> > const ostream_iterator<T> os(cout, "\n");
> > const set<T> words(first, last);
> > copy(words.begin(), words.end(), os);
> > }
>
> > int main(int argc, char** argv) {
> > const istream_iterator<T> is(cin), eof;
> > do_something(is, eof);
> > return 0;
> > }
>
> > The program extracts all the words from an istream (cin) and does
> > something with them. Each word is seperated by a white space by
> > default. The logic behind the formatted extraction is inside the
> > istream_iterator.
>
> > What I need to do now is to pass to do_something() two iterators so
> > that the extracted words will be separated by a punctuation character
> > instead of a white space (white spaces will be considered as "normal"
> > characters). How would you do that in a "clean C++ way" (that is, with
> > the minimum effort)?
>
> The "clean C++ way" would be to imbue your own ctype facet which lets
> you classify what exactly is white space.
>
> /Leigh
thank you Leigh. That's really the thing to do.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web