Using standard Template Library STL to implement file comparison

Source: Internet
Author: User
Tags arrays comparison

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.