Reading from fstream into vector

Discussion in 'C++' started by D. Susman, Sep 17, 2007.

  1. D. Susman

    D. Susman Guest

    Hi,

    In the code snippet below, I am trying to read the contents of a file
    into a vector. However I can't get it compiled. I am using Sunstudio
    which compiles C++ using cc.

    Here is the code:

    #include <fstream>
    #include <vector>

    int main( int argc, char ** argv )
    {
    std::ifstream input( "input.txt" );
    std::istream_iterator<int> dataBegin( input );
    std::istream_iterator<int> dataEnd;
    std::vector<int> v( dataBegin, dataEnd ); //Compiler error

    return (EXIT_SUCCESS);
    }

    Compiler error: Could not find a match for
    std::vector<int>::vector(std::istream_iterator<int, char,
    std::char_traits<char>, int>, std::istream_iterator<int, char,
    std::char_traits<char>, int> ) needed in main(int, char**);
     
    D. Susman, Sep 17, 2007
    #1
    1. Advertising

  2. D. Susman

    Barry Guest

    D. Susman wrote:
    > Hi,
    >
    > In the code snippet below, I am trying to read the contents of a file
    > into a vector. However I can't get it compiled. I am using Sunstudio
    > which compiles C++ using cc.
    >
    > Here is the code:
    >
    > #include <fstream>
    > #include <vector>


    #include <iterator> // for istream_iterator

    >
    > int main( int argc, char ** argv )
    > {
    > std::ifstream input( "input.txt" );
    > std::istream_iterator<int> dataBegin( input );
    > std::istream_iterator<int> dataEnd;
    > std::vector<int> v( dataBegin, dataEnd ); //Compiler error
    >
    > return (EXIT_SUCCESS);
    > }
    >
    > Compiler error: Could not find a match for
    > std::vector<int>::vector(std::istream_iterator<int, char,
    > std::char_traits<char>, int>, std::istream_iterator<int, char,
    > std::char_traits<char>, int> ) needed in main(int, char**);
    >


    Anyway, the absence of the <iterator> shouldn't come out such compile
    time error, it's helpless


    --
    Thanks
    Barry
     
    Barry, Sep 17, 2007
    #2
    1. Advertising

  3. D. Susman

    Barry Guest

    D. Susman wrote:
    > Hi,
    >
    > In the code snippet below, I am trying to read the contents of a file
    > into a vector. However I can't get it compiled. I am using Sunstudio
    > which compiles C++ using cc.
    >
    > Here is the code:
    >
    > #include <fstream>
    > #include <vector>


    #include <cstdlib> // for EXIT_SUCCESS
    not related to your error

    >
    > int main( int argc, char ** argv )
    > {
    > std::ifstream input( "input.txt" );
    > std::istream_iterator<int> dataBegin( input );
    > std::istream_iterator<int> dataEnd;
    > std::vector<int> v( dataBegin, dataEnd ); //Compiler error
    >
    > return (EXIT_SUCCESS);
    > }
    >
    > Compiler error: Could not find a match for
    > std::vector<int>::vector(std::istream_iterator<int, char,
    > std::char_traits<char>, int>, std::istream_iterator<int, char,
    > std::char_traits<char>, int> ) needed in main(int, char**);
    >



    --
    Thanks
    Barry
     
    Barry, Sep 17, 2007
    #3
  4. D. Susman

    BobR Guest

    D. Susman wrote in message...
    >
    > Hi,
    > In the code snippet below, I am trying to read the contents of a file
    > into a vector. However I can't get it compiled. I am using Sunstudio
    > which compiles C++ using cc.
    > Here is the code:
    >
    > #include <fstream>
    > #include <vector>
    >
    > int main( int argc, char ** argv )
    > {
    > std::ifstream input( "input.txt" );
    > std::istream_iterator<int> dataBegin( input );
    > std::istream_iterator<int> dataEnd;
    > std::vector<int> v( dataBegin, dataEnd ); file://Compiler error
    >
    > return (EXIT_SUCCESS);
    > }
    >
    > Compiler error: Could not find a match for
    > std::vector<int>::vector(std::istream_iterator<int, char,
    > std::char_traits<char>, int>, std::istream_iterator<int, char,
    > std::char_traits<char>, int> ) needed in main(int, char**);



    Might try (tested):

    #include <iterator> // stream_iterator
    #include <vector>
    #include <fstream>
    int main(){
    std::ifstream input( "input.txt" );
    std::vector<int> data;
    std::copy(
    std::istream_iterator<int>( input ),
    std::istream_iterator<int>(),
    std::back_inserter( data ) );
    }

    --
    Bob R
    POVrookie
     
    BobR, Sep 17, 2007
    #4
  5. D. Susman

    werasm Guest

    D. Susman wrote:
    > Hi,
    >
    > In the code snippet below, I am trying to read the contents of a file
    > into a vector. However I can't get it compiled. I am using Sunstudio
    > which compiles C++ using cc.


    You need to include <iterator> for std::istream_iterator.

    Regards,

    Werner
     
    werasm, Sep 17, 2007
    #5
  6. D. Susman

    D. Susman Guest


    > You need to include <iterator> for std::istream_iterator.


    Thanks for the advice, but it still does not compile. I think it's
    something about the CC compiler. With g++ 3.4.3, the compiles just
    fine.

    What can it be about CC which refrains the code from being compiled?
     
    D. Susman, Sep 18, 2007
    #6
  7. D. Susman

    Ian Collins Guest

    D. Susman wrote:
    >> You need to include <iterator> for std::istream_iterator.

    >
    > Thanks for the advice, but it still does not compile. I think it's
    > something about the CC compiler. With g++ 3.4.3, the compiles just
    > fine.
    >
    > What can it be about CC which refrains the code from being compiled?
    >

    <OT>The default standard library is missing template member functions,
    compile with -library=stlport4</OT>

    The best place for Sun CC questions is
    http://forum.java.sun.com/forum.jspa?forumID=850

    --
    Ian Collins.
     
    Ian Collins, Sep 18, 2007
    #7
  8. D. Susman

    D. Susman Guest

    On Sep 18, 9:07 am, Ian Collins <> wrote:
    > D. Susman wrote:
    > >> You need to include <iterator> for std::istream_iterator.

    >
    > > Thanks for the advice, but it still does not compile. I think it's
    > > something about the CC compiler. With g++ 3.4.3, the compiles just
    > > fine.

    >
    > > What can it be about CC which refrains the code from being compiled?

    >
    > <OT>The default standard library is missing template member functions,
    > compile with -library=stlport4</OT>
    >
    > The best place for Sun CC questions ishttp://forum.java.sun.com/forum.jspa?forumID=850
    >
    > --
    > Ian Collins.


    Thanks, that is where I am just heading to.
     
    D. Susman, Sep 18, 2007
    #8
  9. D. Susman

    James Kanze Guest

    On Sep 18, 8:07 am, Ian Collins <> wrote:
    > D. Susman wrote:
    > >> You need to include <iterator> for std::istream_iterator.


    > > Thanks for the advice, but it still does not compile. I
    > > think it's something about the CC compiler. With g++ 3.4.3,
    > > the compiles just fine.


    > > What can it be about CC which refrains the code from being
    > > compiled?


    > <OT>The default standard library is missing template member functions,
    > compile with -library=stlport4</OT>


    Which is buggy almost to the point of being unusable, and may
    not be compatible with third party libraries. (If you aren't
    concerned about compatibity with third party libraries, of
    course, then g++ works quite well: the compiler is somewhat
    better than Sun CC to begin with, and the library is an order of
    magnitude better than either of the libraries provided with Sun
    CC. Or use Comeau with the Dinkumware library, so you have a
    standard conformant compiler.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 18, 2007
    #9
  10. D. Susman

    Ian Collins Guest

    James Kanze wrote:
    > On Sep 18, 8:07 am, Ian Collins <> wrote:
    >> D. Susman wrote:
    >>>> You need to include <iterator> for std::istream_iterator.

    >
    >>> Thanks for the advice, but it still does not compile. I
    >>> think it's something about the CC compiler. With g++ 3.4.3,
    >>> the compiles just fine.

    >
    >>> What can it be about CC which refrains the code from being
    >>> compiled?

    >
    >> <OT>The default standard library is missing template member functions,
    >> compile with -library=stlport4</OT>

    >
    > Which is buggy almost to the point of being unusable, and may
    > not be compatible with third party libraries.


    Not buggy, just old. They released it before the compiler supported
    template member functions in class templates. They were then stuck with
    ABI backwards compatibility and so the STLPort library was added.

    > (If you aren't
    > concerned about compatibity with third party libraries, of
    > course, then g++ works quite well: the compiler is somewhat
    > better than Sun CC to begin with, and the library is an order of
    > magnitude better than either of the libraries provided with Sun
    > CC.


    Not in my experience but that's off topic.

    --
    Ian Collins.
     
    Ian Collins, Sep 18, 2007
    #10
  11. D. Susman

    James Kanze Guest

    On Sep 18, 11:53 am, Ian Collins <> wrote:
    > James Kanze wrote:
    > > On Sep 18, 8:07 am, Ian Collins <> wrote:
    > >> D. Susman wrote:
    > >>>> You need to include <iterator> for std::istream_iterator.


    > >>> Thanks for the advice, but it still does not compile. I
    > >>> think it's something about the CC compiler. With g++ 3.4.3,
    > >>> the compiles just fine.


    > >>> What can it be about CC which refrains the code from being
    > >>> compiled?


    > >> <OT>The default standard library is missing template member functions,
    > >> compile with -library=stlport4</OT>


    > > Which is buggy almost to the point of being unusable, and may
    > > not be compatible with third party libraries.


    > Not buggy, just old.


    Which, the STL port, or the Rogue Wave version of the library
    they use by default?

    > They released it before the compiler supported template member
    > functions in class templates. They were then stuck with ABI
    > backwards compatibility and so the STLPort library was added.


    My experience is that 1) the Rogue Wave library is old, as you
    say. It also has one or two very serious bugs (like
    systematically writing one beyond the end of an array), and has
    very, very serious performance problems. The STL port is more
    up to date, and doesn't have the performance problems (or more
    accurately, I've not encountered them, perhaps because we don't
    use it in production), but has even more bugs than the Rogue
    Wave library. Neither begin to come close to Dinkumware, or
    recent versions of g++, in quality.

    > > (If you aren't
    > > concerned about compatibity with third party libraries, of
    > > course, then g++ works quite well: the compiler is somewhat
    > > better than Sun CC to begin with, and the library is an order of
    > > magnitude better than either of the libraries provided with Sun
    > > CC.


    > Not in my experience but that's off topic.


    For many years, I swore by Sun CC. It was my reference
    compiler. But at some point, Sun obviously decided to put all
    their resources behind Java, and let C++ just ride. I think
    (from things people I know at Sun have said) that in the last
    year or two they've revised this again, and are trying to make a
    competative C++ compiler, but they've still got some catching up
    to do. We use Sun CC (with its default library) when we have to
    link with third party libraries, because that's what they used
    when compiling the library. We tend to use g++ otherwise. (And
    of course, on our Linux platforms, it's all g++. And Windows is
    VC++, but those are generally different applications.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 19, 2007
    #11
    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. Row
    Replies:
    0
    Views:
    490
  2. Armando
    Replies:
    6
    Views:
    752
    Armando
    Jan 29, 2004
  3. pmatos
    Replies:
    6
    Views:
    23,852
  4. Replies:
    8
    Views:
    1,939
    Csaba
    Feb 18, 2006
  5. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
Loading...

Share This Page