To read this article, you should be familiar with C + +, familiar with class templates and function templates. This article brings together a lot of relevant information to guide you through the gradual reading.
This article guides reading with questions, designs and solutions. I hope you like it.
Questions raised:
There are two articles that contain many lines of text. We're going to set up a program to figure out the difference between the two and show the rows of the different content. The program must be made into reusable components, that is, this component can be used without modification by other programs.
Design:
Assuming that these two files are very large (thousands of lines per file), we design the solution like this:
Read each file into a block of memory
To compare file contents in a block of memory,
Place the difference into a new third block of memory.
The design also takes into account that the element locations of each file may be different, that is, the same elements are not necessarily in the same row. This means that the search for different terms in memory must be traversed and stored in a third block of memory.
Considering the reusability of the program, we use the generic programming technology to design the solution to adapt to the change of storage medium.
When files are large (thousands of lines per file), it may not be realistic to store each file in memory. In addition, it also brings difficulties to the implementation process.
Implementation Details:
You can use containers to design, such as arrays or queues, to store character arrays in a container. However, this can reduce the readability of the program and cause the reusability of the component to fall.
The solution for this article is to manage memory blocks using the container of the Standard Template Library (Standard Template Library, STL). and use the STL element to manage to read the file into the memory block. This design allows the program to have a level of readability at the template container level.
In order to achieve the goal of interoperability, it is necessary to use C + + class template and function template technology to achieve. If you are unfamiliar with these templates or want to review them, see the link at the end of the article.