Location>code7788 >text

Standard Template Libary or C++ Standard Library

Popularity:777 ℃/2024-07-21 19:19:37

C++ provides a set of standard libraries called C++ standard library, which is also known as Standard Template Library because it is completely completed with templates. This library is specialized in implementing commonly used structs (e.g., ary, list......) and common algorithms (e.g. push, pop, insert, delete, query, retrieval......). The

In general, the STL contains six main components:

(generalized containers)

algorithms (generalized algorithms)

(generalized pointer)

objects

The following briefly describes the first three components to give you an idea of the powerful resilience of templates.

I. STL Cintainers

The STL implements a number of commonly used data structures that are used to house much of the data, so called containers, which fall into two main categories:

1. sequence container: contains ordered elements of the same type. Examples include vectors (a.k.a. arrays) and lists, as well as deques, which behave like vectors but are particularly efficient at insertion and deletion of the first element;

container: This data structure is particularly efficient for querying or retrieving an element. Examples include map and set, which are key/value pairs: the key is used for querying, and the value contains the actual data. Phone books and dictionaries are good candidates for maps, which is why they are also called dictionaries.

Each key in mapt and set can only occur once; if the key must occur more than once, the so-called multimap and multiset apply.

Below I give a vector example to show you the resilience of template classes.

 1 #include <vector> // STL's vector
 2 #include <iostream>
 3 using namespace std;
 4 void main(){
 5     vector<int> ivec;
 6     cout << "ivec: size: " << () << " "
 7     << "capacity: " << () << endl;
 8      for (int ix = 0; ix < 24; ix++)
 9         {
10             ivec.push_back( ix );
11             cout << "ivec: size: " << () << " "             
12           "capacity: " << () << endl;
13          }
14  }               

Program Parsing:

Line 1: The header file <vector> must be imported to use STL vector;

Line 3: All classes, functions, templates, and types in the STL are defined in a special namespace std, and the purpose of line 3 is to allow our program to recognize that std namespace.

Line 5: Declare a vector with the name ivec that can hold elements of data type int inside.

Lines 6 and 7: Before adding any elements to the ivec, check its size and capacity, by size we mean the number of elements in the vector, and by capacity we mean how many elements the vector can currently hold.

Line 8~13: add 24 elements to ivec, check the size and capacity of ivec for each element added, here we use three member functions of vector: push_back(), size(), capacity().

Implementation results:

ivec: size: 0 capacity: 0
ivec: size: 1 capacity: 1
ivec: size: 2 capacity: 2
ivec: size: 3 capacity: 4
ivec: size: 4 capacity: 4
ivec: size: 5 capacity: 8
ivec: size: 6 capacity: 8
ivec: size: 7 capacity: 8
ivec: size: 8 capacity: 8
ivec: size: 9 capacity: 16
ivec: size: 10 capacity: 16
ivec: size: 11 capacity: 16
ivec: size: 12 capacity: 16
ivec: size: 13 capacity: 16
ivec: size: 14 capacity: 16
ivec: size: 15 capacity: 16
ivec: size: 16 capacity: 16
ivec: size: 17 capacity: 32
ivec: size: 18 capacity: 32
ivec: size: 19 capacity: 32
ivec: size: 20 capacity: 32
ivec: size: 21 capacity: 32
ivec: size: 22 capacity: 32
ivec: size: 23 capacity: 32
ivec: size: 24 capacity: 32

  We can generalize two conclusions from the implementation results:

The capacity is variable;

The change in capacity is accrued with power of 2;

II. STL Generic Algorithm

To deal with the STL's various containers, as well as C++'s own built-in data types, the STL provides a set of so-called generic algorithms. They are called algorithms because they are used to perform the most common actions, such as sorting, searching, maximizing, minimizing, and so on. They are called generic because they are applicable to a variety of container types, including STL vectors, lists, sets, maps, and the language's built-in array type.

There are many generic algorithms provided by the STL, such as a simple "reverse string order" example:

 1 #include <>
 2 #include <algorithm> // STL
 3 #include <string.h>
 4 using namespace std; // STL
 5 void main(){
 6     char* string1 = "ABCBEFGHJK";
 7     cout << string1 << endl;
 8     int N1 = strlen(string1);
 9     reverse(string1, string1 + N1);
10     cout << string1 << endl;
11  }

Implementation results:

ABCDEFGHJK

KJHGFEDCBA

Program Parsing:

Line 2: use generic algorithm, must import <algorithm> header file;

Line 4: All classes, functions, templates, and types in the STL are defined in a special namespace std, and the purpose of line 4 is to allow our program to recognize that std namespace.

Line 9: The string is reversed using reverse(), a generic algorithm whose two arguments are the iterators of the operation object (some container), granted to the first and last elements, respectively. The iterator is a generic pointer.

Third, STL Iterators (iterators)

STL containers are very resilient, allowing you to place custom types of data.The STL Container is able to make connections to the STL generic algorithm by virtue of its pair of iterators, which allows the generic algorithm to move back and forth on the container.

The so-called iterator, you can think of it as a kind of generic pointer, its role is to provide generalized methods, so that we can access any kind of container in any of the elements. Iterator is actually a template class, it must provide a minimum of four operators:

operator: used to move to the next element, e.g. ++iter.

operator: used to take out the real individual element, e.g. *iter.

operator: used to determine whether two iterators are equal, such as iter1 == iter2.

operator: used to determine whether two iterators are unequal, e.g.: iter1 ! =iter2.

Any STL container type must provide two member functions: begin() and end(), and the passed back iterator points to the first and the last element of the container, respectively. The following is a demonstration of the previous vector<int>ivec as an example The following is a demonstration of the declaration and application of iterator with the previous vector<int>ivec example:

1 vector<int> ivec;
2 ...
3 vector<int>::iterator iter = ();
4 vector<int>::iterator iter_end = ();
5 for (; iter != iter_end; ++iter)
6 cout << *iter << endl;

The final for loop displays all the elements inside the ivec to the screen;