1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Programming Tutorial - C++ Strings

By David Bolton, About.com

10 of 10

Example 8 Continued - Processing the File

The method extractlinks() does the bulk of the work. It finds the start and end of a link through calls to find(), then calls the push_back() method. This adds an element on the end of the list. This loop uses the variable pos to limit the search. When extractlinks() finishes, the links list is complete.

The last part of the textfile constructor calls the sort() function. This is an STL algorithm which works on the list and sorts the words alphabetically. It just requires two iterators links.begin() and links.end().

When I ran this on the cplus.about.com page that I had saved to disk it found 149 links. Don't forget if you use a path like c:\folder\file.htm that you must put it in the constructor as "c:\\folder\\file.htm".

Conclusion

This has been written for simplicity and could be improved considerably, however I wanted to show use of strings not the power of the STL. There are other classes such as streambuf, stringbuf and filebuf, all part of the iostream library that can make life even easier and in the next tutorial I'll look at those.

That completes this tutorial. The next one will be on file I/O.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.