By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

Reading from fstream into vector

P: n/a

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<intdataBegin( input );
std::istream_iterator<intdataEnd;
std::vector<intv( 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**);

Sep 17 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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<intdataBegin( input );
std::istream_iterator<intdataEnd;
std::vector<intv( 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 <iteratorshouldn't come out such compile
time error, it's helpless
--
Thanks
Barry
Sep 17 '07 #2

P: n/a
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<intdataBegin( input );
std::istream_iterator<intdataEnd;
std::vector<intv( 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
Sep 17 '07 #3

P: n/a

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<intdataBegin( input );
std::istream_iterator<intdataEnd;
std::vector<intv( 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<intdata;
std::copy(
std::istream_iterator<int>( input ),
std::istream_iterator<int>(),
std::back_inserter( data ) );
}

--
Bob R
POVrookie
Sep 17 '07 #4

P: n/a

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

Regards,

Werner

Sep 17 '07 #5

P: n/a
You need to include <iteratorfor 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?

Sep 18 '07 #6

P: n/a
D. Susman wrote:
>You need to include <iteratorfor 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.
Sep 18 '07 #7

P: n/a
On Sep 18, 9:07 am, Ian Collins <ian-n...@hotmail.comwrote:
D. Susman wrote:
You need to include <iteratorfor 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.

Sep 18 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.