Reading from fstream into vector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Susman

    #1

    Reading from fstream into vector


    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_it erator<intdataB egin( input );
    std::istream_it erator<intdataE nd;
    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_iterato r<int, char,
    std::char_trait s<char>, int>, std::istream_it erator<int, char,
    std::char_trait s<char>, int) needed in main(int, char**);

  • Barry

    #2
    Re: Reading from fstream into vector

    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_iterato r
    >
    int main( int argc, char ** argv )
    {
    std::ifstream input( "input.txt" );
    std::istream_it erator<intdataB egin( input );
    std::istream_it erator<intdataE nd;
    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_iterato r<int, char,
    std::char_trait s<char>, int>, std::istream_it erator<int, char,
    std::char_trait s<char>, int) needed in main(int, char**);
    >
    Anyway, the absence of the <iteratorshould n't come out such compile
    time error, it's helpless


    --
    Thanks
    Barry

    Comment

    • Barry

      #3
      Re: Reading from fstream into vector

      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_it erator<intdataB egin( input );
      std::istream_it erator<intdataE nd;
      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_iterato r<int, char,
      std::char_trait s<char>, int>, std::istream_it erator<int, char,
      std::char_trait s<char>, int) needed in main(int, char**);
      >

      --
      Thanks
      Barry

      Comment

      • BobR

        #4
        Re: Reading from fstream into vector


        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_it erator<intdataB egin( input );
        std::istream_it erator<intdataE nd;
        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_iterato r<int, char,
        std::char_trait s<char>, int>, std::istream_it erator<int, char,
        std::char_trait s<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_it erator<int>( input ),
        std::istream_it erator<int>(),
        std::back_inser ter( data ) );
        }

        --
        Bob R
        POVrookie


        Comment

        • werasm

          #5
          Re: Reading from fstream into vector


          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 <iteratorfor std::istream_it erator.

          Regards,

          Werner

          Comment

          • D. Susman

            #6
            Re: Reading from fstream into vector

            You need to include <iteratorfor std::istream_it erator.
            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?



            Comment

            • Ian Collins

              #7
              Re: Reading from fstream into vector

              D. Susman wrote:
              >You need to include <iteratorfor std::istream_it erator.
              >
              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=stlport 4</OT>

              The best place for Sun CC questions is


              --
              Ian Collins.

              Comment

              • D. Susman

                #8
                Re: Reading from fstream into vector

                On Sep 18, 9:07 am, Ian Collins <ian-n...@hotmail.co mwrote:
                D. Susman wrote:
                You need to include <iteratorfor std::istream_it erator.
                >
                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=stlport 4</OT>
                >
                The best place for Sun CC questions ishttp://forum.java.sun. com/forum.jspa?foru mID=850
                >
                --
                Ian Collins.
                Thanks, that is where I am just heading to.

                Comment

                Working...