1. Computing & Technology

Discuss in my forum

C++ Programming Tutorial - C++ Strings

By , About.com Guide

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.

©2012 About.com. All rights reserved.

A part of The New York Times Company.