Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

STL - vector<> and istream

226 views
Skip to first unread message

Opi

unread,
Jan 9, 2002, 8:44:04 AM1/9/02
to
Hi, I learned STL a little, but now I found a problem that I can't deal
with:
While testing the sort algorithm (8 000 000 integers), I needed to get some
random values from a file that my co-worker has. So we selected a movie, and
now i need to read 32 MB from it. Well I think there's better way than
for(...) vect1.push_back(&data); I mean something like combinating istream
and vector. But I can't find the way to do it - how can I create an iterator
for istream? Then I could use vector::insert() .

So please some simple-sample for reading from istream via iteraror, thanks a
lot! Opi


Jon Bills

unread,
Jan 9, 2002, 9:01:18 AM1/9/02
to
"Opi" <o...@volny.cz> wrote in message news:a1hhgp$1lo$1...@ns.felk.cvut.cz...

> Hi, I learned STL a little, but now I found a problem that I can't deal
> with:
> While testing the sort algorithm (8 000 000 integers), I needed to get some
> random values from a file that my co-worker has. So we selected a movie, and
> now i need to read 32 MB from it. Well I think there's better way than
> for(...) vect1.push_back(&data); I mean something like combinating istream
> and vector. But I can't find the way to do it - how can I create an iterator
> for istream? Then I could use vector::insert() .

Like, for instance, istream_iterator?

>
> So please some simple-sample for reading from istream via iteraror, thanks a
> lot! Opi

An example:

#include <vector>
#include <iterator>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
typedef int elem;
vector<elem> v;

ifstream in("numbers.txt") ;

copy( istream_iterator<elem>(in), istream_iterator<elem>(),
back_inserter(v)) ;

return 0 ;
}

Note: istreambuf_iterator is also useful if you want unformatted input.

Jon.


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

Opi

unread,
Jan 9, 2002, 3:40:27 PM1/9/02
to
I don't have anything about this in my STL help :( Thanks a lot, leaving
for MSDN or some online help...

"Jon Bills" <jon_...@hotmail.com> píse v diskusním príspevku
news:f87960cc00d333b2490...@mygate.mailgate.org...

Mike Wahler

unread,
Jan 9, 2002, 4:43:06 PM1/9/02
to
Opi <o...@volny.cz> wrote in message news:a1i9ta$e0n$1...@ns.felk.cvut.cz...

> I don't have anything about this in my STL help :( Thanks a lot, leaving
> for MSDN or some online help...

A good online C++ standard library reference that I like is:
http://www.dinkumware.com/htm_cpl/index.html

.. which of course is no substitute for a good book.
For that, I recommend:
http://www.josuttis.com/libbook/index.html

-Mike

Opi

unread,
Feb 2, 2002, 4:38:03 PM2/2/02
to
Ok, so I wrote the following -vvv- - is that the right way to copy ints
from a file at once? I think it's not as it doesn't work - adds nothing to
the vector :(
And what if I want to read exact number of ints? (in my case, 8 000 000.)
And how should I use vector::insert() here, if it's even possible? Thanks
Opi
--------------------------------------------------------
#define VI vector<int>
using namespace std;
int main(int argc, char* argv[]){

printf("Mackni neco.\n"); getch();
srand((unsigned int)time(0));

const N = 800000;
vector<int> v;
v.reserve(N);
vector<int>::iterator i1 = v.begin();
vector<int>::iterator i2 = v.end();

// ... normal way, commented out
/*FILE *stream = fopen("C:\\!Kyri\\p\\avseq02.mpg", "rb");
int n_int = 8*1000*1000; // to je rozepsaný jen aby to bylo prehlednejsí
pác 800000 == ??? na první pohled
int *array = (int *)malloc(n_int * sizeof(int));
fread(array, sizeof(int), n_int, stream);*/

// naplneni
/*size_t kolik = 1; // int data;
for(int i=N-1; i && kolik;i--){
//kolik = fread(&data,sizeof(int),1,fp);
v.push_back(array[i]);
}*/

std::basic_ifstream<int> fs;
istream_iterator<int,int> isi;
fs.open("C:\\!KYRI\\P\\AVSEQ02.mpg",ios_base::in);
//v.insert(v.end(),fs.begin(),fs.end()); // <--- v
copy(istream_iterator<int,int>(fs),
istream_iterator<int,int>(),back_inserter(v)) ;
fs.close();

.....
--------------------------------------------


"Jon Bills" <jon_...@hotmail.com> píse v diskusním príspevku
news:f87960cc00d333b2490...@mygate.mailgate.org...

0 new messages