So please some simple-sample for reading from istream via iteraror, thanks a
lot! Opi
> 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
"Jon Bills" <jon_...@hotmail.com> píse v diskusním príspevku
news:f87960cc00d333b2490...@mygate.mailgate.org...
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
 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...