Reading from fstream into vector

D

D. Susman

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

Barry

D. Susman said:
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 said:
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
 
B

Barry

D. Susman said:
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**);
 
B

BobR

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 ) );
}
 
W

werasm

D. Susman said:
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
 
D

D. Susman

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

Ian Collins

D. Susman said:
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
 
D

D. Susman

<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

Thanks, that is where I am just heading to.
 
J

James Kanze

<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.)
 
I

Ian Collins

James said:
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.
 
J

James Kanze

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.
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top