Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading from fstream into vector

Reply
Thread Tools

Reading from fstream into vector

 
 
D. Susman
Guest
Posts: n/a
 
      09-17-2007

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**);

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-17-2007
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
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-17-2007
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
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      09-17-2007

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


 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      09-17-2007

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

 
Reply With Quote
 
D. Susman
Guest
Posts: n/a
 
      09-18-2007

> 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?



 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2007
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.
 
Reply With Quote
 
D. Susman
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 9:07 am, Ian Collins <(E-Mail Removed)> 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.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 8:07 am, Ian Collins <(E-Mail Removed)> 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:(E-Mail Removed)
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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2007
James Kanze wrote:
> On Sep 18, 8:07 am, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Reading Blank Lines into StringTokens into Vector Array Row Java 0 04-14-2004 07:12 AM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM



Advertisments