C ++ & quot; Class templates and STL Programming & quot;, template stl

Source: Internet
Author: User

C ++: "class templates and STL programming", template stl
Class template ConceptI. Class templatesClasses of different types of members can be created based on different parameters.

Ii. class template syntax:Template <template parameter table>
Class Name
{
Member name;
};

Iii. class template instantiation1. Syntax: Class Template Name <template parameter value table> Object 1, object 2,..., object n;
2. features: the value of the template parameter value table is a type name. The type name can be a basic data type name, a constructed data type name, or a class type name.
The value of the template parameter value table can also be a constant expression.
The template parameter table of the class template is instantiated in a one-to-one correspondence order.

Iv. Stack1. Definition: a structure of First In Last Out
2. Basic operations: push the stack, pop the stack, empty judge, full judge, and read the top element of the stack.

V. STL1. Definition: the standard template library is an efficient C ++ library.
2. role: it embodies the idea of generic programming and advocates the use of existing template program code to develop applications. It is a code reuse technology. It can improve the labor productivity and target system quality of software developers and is an important goal pursued by software engineering.

Vi. STL containerSTL containers can be divided into two types: sequential containers and associated containers.
1. Ordered container:Stores an element sequence in a logically linear arrangement. Objects in these container types are logically considered to be stored in consecutive surviving spaces.
(1) vector:You can use constant time to access and modify any element. It has a constant time complexity when inserting or deleting the end of the sequence, the time complexity of inserting and deleting any item is proportional to the distance to the end, especially the cost of adding and deleting vector headers is astonishing.
Features: fast search is required, so you do not need to care about the speed of insertion or deletion. Vectors can be used wherever arrays are available. Header file: <vector>(2) list:Access to any element is proportional to the distance between the two ends, but it takes a constant time to insert or delete an item at a certain position. Features: Fast insertion or deletion is required, regardless of the search speed.
Header file: <list>
(3) deque:It does not occupy a continuous space in the memory. It is closer to the vector between the vector and the list, and is suitable for data access at both ends. You can use the [] operator to directly access data.
Features: Quick element access. It is slow to insert or delete a sequence. Generally, you do not need to use a dual-end queue. Instead, you can use a vector or list.
Header file: <deque>
2. Associated containers:A keyword-to-value Association ing is provided, and is not stored in the ordered linear data structure.
(1) set and multiset ):Set is a collection of elements. Elements in the set are stored in an ordered manner. There are no repeated elements in the set, but repeated elements are allowed in the multiset.
Feature: You can use set/multiset when you need to use element sets and perform frequent operations on element search, insertion, and deletion.
Header file: <set>(2) map and multimap ):Map is a set of {key (key), value} pairs. Key-based arrangement of elements in the set. Multimap is a set that allows duplicate key and value pairs. The relationship between map and multimap is the relationship between set and multiset.
Features: map/multimap can be used to associate keys and values.
Header file: <map>

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.