473,881 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::search question...


Can anybody help me with the following piece of code?

The purpose behind the code is to parse HTML files, strip out
the tags and return the text between tags. This is part of a larger
application which will perform "searches" for text values in a
directory of html files, trying to match only the non-tagged text
in the documents.

My approach was to customize a istreambuf_iter ator which would
iterate over the text characters in the HTML bodies, and ignore the html
tags. I did
this by deriving from the std istreambuf_iter ator, overriding the ++
operators to parse the underlying input to screen the data and return the
next valid character. The iteration seems to work ok on some simple
HTML test data - I've seen the tags stripped away and the text body get

Now here's the clever part :). I thought I could use the std algorithm
directly, plug in the range of the stream and the range of the text string
look for, and there you go. Unfortunately, this doesn't work ! The search
always fails to find the search string.

Am I missing something about the search? I thought the requirements for
search only required the iterators be forward iterators - is there something
misunderstood here ? I'd like to use this code, it strikes me as a nice
utilizing the power of STL and the stream classes.

Many thanks for any help!


#include <algorithm>
#include <fstream>
#include <istream>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class htmlstreambuf_i terator : public istreambuf_iter ator<char>
htmlstreambuf_i terator( ifstream& stream)
:istreambuf_ite rator<char>(str eam),_intag(fal se)
// advance to first "legal" character in input.
!istreambuf_ite rator<char>::eq ual(istreambuf_ iterator<char>( )) )

char c = istreambuf_iter ator<char>::ope rator*();
if ( c == '<' )

else if (!_intag )




htmlstreambuf_i terator( )
:istreambuf_ite rator<char>(), _intag(false)
htmlstreambuf_i terator( const htmlstreambuf_i terator& iter)
:istreambuf_ite rator<char>( iter), _intag(false)

char operator*()
printf("operato r*() %c\n", istreambuf_iter ator<char>::ope rator*() );
return istreambuf_iter ator<char>::ope rator*();


const char* operator->()
return istreambuf_iter ator<char>::ope rator->();

htmlstreambuf_i terator& operator++()
return *this;
htmlstreambuf_i terator operator++(int)
htmlstreambuf_i terator tmp=*this;
return tmp;


bool equal( htmlstreambuf_i terator& rhs )
return istreambuf_iter ator<char>::equ al(rhs);

void incr()
while( !istreambuf_ite rator<char>::eq ual(istreambuf_ iterator<char>( )) )
// advance and skip over any tag data to next legal character.
istreambuf_iter ator<char>::ope rator++();
char c = istreambuf_iter ator<char>::ope rator*();
if ( c == '<' && !_intag )
_intag = true;

else if ( c == '>' && _intag )
_intag = false;
else if (!_intag )



bool _intag;


int main(int argc, char* argv[])
ifstream input( argv[1] );

htmlstreambuf_i terator iter(input);
htmlstreambuf_i terator iterend;

string searchtext("D") ;
htmlstreambuf_i terator pos = search( iter, iterend,

searchtext.begi n(), searchtext.end( ) );

// pos always points to end of input. !!!
bool found = pos != iterend ;
return 0;

Jul 23 '05 #1
1 3062
Dave Townsend wrote:
My approach was to customize a istreambuf_iter ator which would
iterate over the text characters in the HTML bodies, and ignore the html tags. I did
this by deriving from the std istreambuf_iter ator, overriding the ++
operators to parse the underlying input to screen the data and return the next valid character.
You would be better off wrapping 'istreambuf_ite rator' into a custom
iterator class as this class is not intended to be derived from. In
particular, you did not override 'operator++()' but overload this
operator: 'std::istreambu f_iterator' does not have any virtual
and thus you cannot override anything. A class using public inheritance
from 'std::istreambu f_iterator' is rather error prone and subject e.g.
to slicing (e.g. when being passed to a function taking an
'std::istreambu f_iterator' as argument).
Am I missing something about the search?
Yes, you do: 'std::search()' takes forward iterators, not input
iterators. However, 'std::istreambu f_iterator' is an input iterator.
It is not too hard to create a wrapper for 'std::istreambu f_iterator'
which allows forward iteration (in particular allows the possibility
to recover to some position).
I thought the requirements for
search only required the iterators be forward iterators - is there something I've misunderstood here?

Your misunderstandin g seems to be with 'std::istreambu f_iterator':
this is not a forward iterator. It is an input iterator. The key
difference is that input iterators only support single pass while
forward iterators allow multiple passed (plus the possibility to
write to the sequence which is not needed in this case).
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 23 '05 #2

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

Similar topics

by: Peter Merz | last post by:
Englisch: Rear one, I have the following problem, I a program wrote, in which one cannot look for it from above downward clicks one in the field however from down upward, goes. I must sketch now an algorithm, which scans a text upward, there my program (power ++ of Sybase) such a function as standard equipment do not provide. The code for forward-searches:
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able replace baker replace charlie "
by: sparks | last post by:
We have a person that has 4 columns and 20 rows of data. when they have all the info they want to put in the data a row at a time. BUT if they only have the first column of data they want to enter that. with tab orders you can set it to go across or down each column. He wanted to know if he could use the down arrow to put in the one column. has anyone ever faced this or heard about someone wanting this, and how you
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used to do this? Here is a concrete example: If I have a string with sequences of CRLF and possibly just CR's, is there a simple way of replacing the CRLF characters with a LF? If you look at the function below is it possible to use a replace_if...
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
by: ma740988 | last post by:
I've been perusing for some time now - I suppose some of the algorithms in Josuttis. 'Today', I do alot of funny math on data resident within memory. As part of this. I find myself using std::search_n alot to find a particular value in a range of memory. I'm fairly pleased with std::search_n - in that my benchmark ( below ) shows on average an 8 second performance gain comparable to a for loop. The question. Since I haven't quite...
by: B. Williams | last post by:
I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called updatePower which will search through the file looking at the names and if a name is a match, will allow you to replace the integer stored for power. I have written the program and completed the first task, but I need some assistance on the secind....
by: desktop | last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000 operations to find element with value = 1.000.000 (need to iterator through the whole list). In comparison, if I have a std::set with 1.000.000 element it will only take approx lg 1.000.000 = 20 operations! Can it really be true that the difference is a factor of 1.000.000/20 = 50.000 in this case?
by: Lambda | last post by:
Hi, All, Why the search() function use Forward Iterator? I think Input Iterator is enough, I need read only. In such kind of situations, how should I choose iterator type?
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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,...
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...
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...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.