[CPP] [generic-programming] generic programming and STL

Source: Internet
Author: User
Tags types of functions

Process-oriented, object-oriented, and generic programming (GP) should be three reusable programming methods. In traditional C ++ languages, generic programming is only embodied in simple template technology. The standard template library (STL) introduced later is the actual embodiment and concrete implementation of generic programming ideas.

1. Problem Introduction

A process-oriented method can encapsulate common code segments in a function and reuse the target code through function calls.
Object-oriented methods can be used to reuse (Object goals) code through class inheritance.

To compile an algorithm that can be used for different data types, you can use the following methods:
1) process-oriented method: source code is copied and modified to generate Algorithm functions of different data types. Manual judgment of data types is required during calls;
2 ). object-Oriented Methods: write multiple functions with the same name in a class. Their algorithms are the same, but the types of processed data are different. Of course, the input parameter types of functions are also different, function overload to automatically call the function of the corresponding data type version.

Obviously, the above two methods both need to write different functions with the same algorithm, and cannot be reused in code. The main difference between them is the convenience of calling. If generic programming is used (for example, the traditional C ++ template technology that can be used as a parameter), source code reuse can be achieved:

A generic programming method is used to compile a template function that uses a type as a parameter. When called, the parameter is instantiated as a specific data type. To implement such an algorithm, it has different organizational structures (such as arrays, linked lists, queues, stacks, etc), including data or objects of the same type (such as char, Int, float, struct s, or Class C, common parameterization algorithms unrelated to specific data types (such as sorting, retrieval, copying, and merging ). Only templates are far from enough, it also needs containers that can represent such sets, iterators that can traverse in containers, splitters that can implement abstract storage for algorithms and containers, and adapters that can be converted between different containers. These are the research contents of generic programming and the goal of STL.

2. Generic programming

Generic programming (generic) is a technology that allows a value to take different data types. Based on this technology, the most efficient and abstract representation programming method is called Generic programming (generic programming, general programming/generic programming ).

Generic programming focuses on generating general software components that can be easily reused in different application scenarios. In C ++, class templates and function templates are extremely effective mechanisms for generic programming. unlike the problem-oriented and data-oriented object-oriented methods, generic programming emphasizes algorithms and is a type of common parameterized algorithms, they work in the same way for various data types and various data structures to achieve source code-level software reuse.

For example, whether the elements (types) in a container are arrays, queues, linked lists, or stacks, whether they are characters, integers, floating-point numbers, or objects, you can use the same (iterator) method to traverse all elements in the container, obtain the value of a specified element, and add or delete elements, this allows you to perform operations and algorithms such as sorting, retrieval, copying, and merging.

Generic programming generic algorithms are based on various abstractions: parameterization templates are used to abstract data types, containers and iterators are used to abstract data structures, and splitters and adapters are used to abstract storage allocation and interface interfaces.

3. STL

STL (Standard Template Library) is the actual embodiment and implementation of generic programming ideas. It is a scalable architecture that establishes a large standard library for generic components. STL itself has nothing to do with object-oriented programming languages. The objective of STL is to abstract it without losing efficiency. No loss of efficiency here refers to the best effort to ensure that all of its generic algorithms are optimal and have the same operating efficiency as manual coding.

The essence of STL is that different data structures correspond to different address algebra structures and different address connection methods. Some Column Operations from an address in the data structure to the next address correspond to the iterator. The operation of adding and deleting addresses in the data structure corresponds to the container.

STL regards containers as the generalization of structures. They all have members and can describe the key attributes of the whole and local real-world things. STL uses the value assignment algorithm and requires Value-Oriented semantics. STL also defines the full order of the data and objects in the container. STL consists of six components: container, generic algorithm, iterator, function object, distributor, and adapter. In standard C ++, STL emerged as part of the C ++ standard library.

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.