c++ - How to use container neighbours in std algorithms in a right way -
are std algorithms designed use several neighbor elements in container?
here have done read samples file(each sample has it's own offset)
long offsets[] = { 0x00000000, 0x00000010, 0x00000020, .... } ifstream file(path, ios::binary); vector<sample> samples; for_each(begin(offsets), end(offsets), [](long &offset){ auto samplesize = *(&offset+1)-offset; sample s; file.read( s.getbuffer(), samplesize); samples.push_back(s); });
to figure out size need 2 adjacent elements vector. think use std::advance() or std::next() instead of *(&offset+1). right approach @ here use standard algorithm? if yes, algorithms designed used in way?
with boost zip iterator, may like:
// assume offsets not empty. std::for_each( boost::make_zip_iterator(boost::make_tuple(begin(offsets), begin(offsets + 1))), boost::make_zip_iterator(boost::make_tuple(end(offsets) - 1, end(offsets))), [&](const boost::tuple<const long&, const long&>& t) { auto samplesize = t.get<1>() - t.get<0>(); sample s; file.read(s.getbuffer(), samplesize); samples.push_back(s); } );
Comments
Post a Comment