473,785 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

distance between stream iterators

Evidently per the C++ standard, it is not possible to measure the
distance between two stream iterators, because two non-end-of-stream
iterators are equal when they are constructed from the same stream. I
still don't quite understand why that fact is true. But...

I wish to find the first occurance of some sequence in an existing
stream. For the moment this stream is a file on disk, but later may be
a stream from another process. Anyway, if I read a chunk of the stream
into a container like a vector, I can use the STL search() algorithm to
get an iterator to the sequence being searched for, and then use the
STL distance() algorithm to measure the distance between this iterator
and the beginning of the vector. The beginning of the vector is
directly related to the beginning of the stream, so I know where the
sequence is in the stream. So you'd think I'd be happy. Well sort of.

I'd like to operate directly on the stream, without re-buffering the
data into another container. This seems like a reasonable desire, since
the stream is already buffered by its internal implementation. But the
STL standard for some reason prevents the comparision of istream
iterators. Why? Or more importantly how can I operate directly on the
stream, and get an integer value of the location of the desired
sequence. Sure I have it solved with a vector approach, but that seems
an indirect and unnecessary approach. Below is my test code for this
inquiry.

//stream iterator test
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;
int main()
{
// FAF03200 can be found at byte location 28 in test.bin
// let's see if we can get those same results by operating
// directly on the on the stream, instead of copying first
// into some container.

vector<unsigned char> pattern; // a pattern in file

// store FAF03200 pattern in a sequence vector
std::inserter(p attern,pattern. end())= (unsigned char)0xF0;
std::inserter(p attern,pattern. end())= (unsigned char)0xFA;
std::inserter(p attern,pattern. end())= (unsigned char)0x00;
std::inserter(p attern,pattern. end())= (unsigned char)0x32;

ifstream f;
string filename("test. bin");
f.open (filename.c_str (), ios::binary );

if ( !(f.good()) ){
cout << "error opening file \n";
return -1;
}

//----------- find first occurance of pattern in file ----------
f.seekg (0, ios::beg);

istream_iterato r<unsigned char> first_byte(f);
istream_iterato r<unsigned char> foundAt;

foundAt= std::search( istream_iterato r<unsigned char>(f),
istream_iterato r<unsigned char> (),
pattern.begin() ,pattern.end()) ;

cout << "search complete \n";

//ERROR: distance() always returns zero
long at = distance(first_ byte, foundAt);

cout << "Stream methods says it's at " << at << ".\n";

//-------------- the indirect method works fine but... ------------
char c;
int numBytes=0;
vector<unsigned char> v;

// throw a chunk of file into a unsigned char vector
f.seekg (0, ios::beg);
while( f.get(c) && numBytes++ < 8192 ){
std::inserter(v ,v.end())= c;
}

// find iterator to first sequence in char vector
vector<unsigned char>::iterator pos;
pos = std::search ( v.begin(),v.end (),
pattern.begin() ,pattern.end()) ;

// determine index from the return iterator
int where = distance(v.begi n(),pos); // GREAT: works perfect

cout << "Indirect vector methods says it's at " << where << ".\n";

f.close();
return 0;
}

Nov 22 '05 #1
4 2319
On 2005-11-14, fa********@cox. net <fa********@cox .net> wrote:
Evidently per the C++ standard, it is not possible to measure
the distance between two stream iterators, because two
non-end-of-stream iterators are equal when they are constructed
from the same stream. I still don't quite understand why that
fact is true. But...


You may wish to try to implement your own istream_iterato r-like
template that is a forward iterator instead of an input iterator.
If it turns out to be no trouble, then you'll have what you
wanted. If it turns out that you can't do it, then you'll be
enlightened. That's a win-win.

--
Neil Cerutti
Nov 22 '05 #2

"Neil Cerutti" <le*******@emai l.com> wrote in message
news:sl******** **************@ FIAD06.norwich. edu...
On 2005-11-14, fa********@cox. net <fa********@cox .net> wrote:
Evidently per the C++ standard, it is not possible to measure
the distance between two stream iterators, because two
non-end-of-stream iterators are equal when they are constructed
from the same stream. I still don't quite understand why that
fact is true. But...


You may wish to try to implement your own istream_iterato r-like
template that is a forward iterator instead of an input iterator.
If it turns out to be no trouble, then you'll have what you
wanted. If it turns out that you can't do it, then you'll be
enlightened. That's a win-win.


I just typed in win win and my puter crashed.
Repair cost was O log n. Where do I send the Bill?

Sorry.

-Mike
Nov 22 '05 #3
Distance between stream operators? Why t3h h3ll would you want to know
that?

Give me your ip and maybe I'll show you some of my scripts I
downloaded!

Nov 22 '05 #4
I agree that implementing my own iterator may provide some insight and
would be enlightening, but it will take some thought. I'm concerned
that such an implementation would still involve re-buffering of data
which istream already buffers. The whole point of my concern is
efficiency improvement, by eliminating the copy of data to another
container.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1721
by: Fraser Ross | last post by:
Algorithms cannot be used with input stream iterators? Is copying the range to a temporary container before using the algorithm the usual thing to do? Fraser.
2
4735
by: tron.thomas | last post by:
I have tried the following code on three different compilers and all three produce programs that hang and fail to complete successfully. #include <map> #include <string> #include <iostream> #include <iterator> int main() {
24
3968
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = s2 = s3 = for value in ???(s1, s2, s3):
0
2255
by: gary_dr | last post by:
What's so special about istream_iterator that prevents me from determining the distance between two of them. In the code below, search() does return a foundAt iterator that points to the location in question, but distance() still always returns zero. However reading a chunk of file into a vector and searching the vector produces an iterator that distance() will measure properly. Evidently , it is not possible to measure the distance ...
19
7554
by: John | last post by:
In STL's map implementation, the distance between two iterators it1, it2 takes O(K) to compute where K is the actual distance between the two iterators. In theory, a red black tree can do this in O(log K) time. Anyone knows if there is a way to do this in map<> or if map<> can be inherited/modified to do this? Thanks, --j
10
4739
by: Alan Johnson | last post by:
24.1.1.3 says about InputIterators: Algorithms on input iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms. In 24.3.4.4 summarizes the effects of std::distance as: Effects: Returns the number of increments or decrements needed to get from first to last. The wording of the latter seems somewhat ambiguous. Does it mean that it actually performs the necessary increments or...
9
2325
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the list. For instance, if I did something like distance(list.end(), list.begin()), I would get -list.size(). The STL's iterator distance function amounts to something like this: distance_type distance(Iterator first, Iterator last) {...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.