Help with reading formatted input from an istream

Discussion in 'C++' started by Ttodir, Jan 27, 2012.

  1. Ttodir

    Ttodir Guest

    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)?
    Ttodir, Jan 27, 2012
    #1
    1. Advertising

  2. Ttodir

    Ian Collins Guest

    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
    Ian Collins, Jan 27, 2012
    #2
    1. Advertising

  3. Ttodir

    Ttodir Guest

    On Jan 27, 12:47 pm, Leigh Johnston <> 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.
    Ttodir, Jan 28, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    426
    Eric Reitmaier
    Oct 19, 2004
  2. Nomak
    Replies:
    4
    Views:
    592
    Ali Cehreli
    Aug 16, 2004
  3. Replies:
    3
    Views:
    894
  4. Replies:
    2
    Views:
    476
  5. xmllmx
    Replies:
    5
    Views:
    575
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page