In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to implement the iterator of STL components, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
An important feature of STL is the separation of data structure and algorithm. Although this is a simple concept, this separation does make STL very generic. For example, because STL's sort () function is completely generic, you can use it to manipulate almost any set of data, including linked lists, containers, and arrays.
Keystone
The STL algorithm is provided as a template function. To distinguish it from other components, the STL algorithm is followed by a pair of parentheses in this book, such as sort ().
Another important feature of STL is that it is not object-oriented. In order to be versatile enough, STL mainly depends on templates rather than encapsulation, inheritance and virtual functions (polymorphism)-- the three elements of OOP. You can't find any obvious class inheritance relationships in STL. This seems like a step backwards, but it is precisely the underlying feature that makes STL components universal. In addition, because STL is based on templates, the use of inline functions makes the generated code short and efficient.
Prompt
Make sure that at least-O optimization is used in programs that compile with STL to ensure inline extensions.
STL component
STL provides a large number of template classes and functions that can be used in OOP and general programming. All STL's about 50 algorithms are completely generic and do not depend on any particular data type. The following sections describe the three basic STL components:
1) the iterator provides a way to access objects in the container. For example, you can use a pair of iterators to specify a range of objects in list or vector. An iterator is like a pointer. In fact, the pointer of C++ is also an iterator. However, iterators can also be class objects that define operator* () and other operator-like methods like pointers.
2) A container is a data structure, such as list,vector, and deques, which is provided in the method of a template class. To access the data in the container, you can use an iterator output by the container class.
3) the algorithm is a template function used to manipulate the data in the container. For example, STL uses sort () to sort the data in a vector and find () to search for objects in a list. The functions themselves are independent of the structure and type of data they operate on, so they can be used on any data structure, from simple arrays to highly complex containers.
1. Header file
To avoid conflicts with other header files, STL's header files no longer use the regular .h extension. To include standard string classes, iterators, and algorithms, use the following indicators:
# include # include # include
If you look at the header files of STL, you can see header files like iterator.h and stl_iterator.h. Since these names may vary between STL implementations, you should avoid using these names to refer to these header files. To ensure portability, use the corresponding file name without the .h suffix.
2. Namespace
Your compiler may not recognize namespaces. A namespace is like an envelope, encapsulating the logo in another name. Markers exist only in namespaces, thus avoiding conflicts with other markers. For example, there may be other libraries and program modules that define the sort () function, and to avoid conflicts with STL's sort () algorithm, STL's sort () and other markers are encapsulated in the namespace std. STL's sort () algorithm is compiled to std::sort (), which avoids name conflicts.
Although your compiler may not implement namespaces, you can still use them. To use STL, insert the following indicator into your source code file, typically after all the # include indicators:
Using namespace std
3. Iterator
Iterators provide access to objects in a container and define the scope of objects in the container. An iterator is like a pointer. In fact, the pointer of C++ is also an iterator. However, iterators are not just pointers, so you can't assume that they must have address values. For example, an array index can also be thought of as an iterator.
Iterators can be created in different ways. The program may create an iterator as a variable. A STL container class may create an iterator to use a particular type of data. As a pointer, you must be able to get data using the * operator class. You can also use other mathematical operators such as +. Typically, the + operator is used to increment the iterator to access the next object in the container. If the iterator reaches behind an element in the container, the iterator becomes the past-the- end value. Using a past-the-end worth pointer to access an object is illegal, just like using a NULL or a pointer for initialization.
(1) prompt
STL does not guarantee that an iterator can be reached from another iterator. For example, when sorting objects in a collection, if you specify two iterators in different structures, the second iterator cannot arrive from * iterators, and the program is doomed to failure. This is a price for STL flexibility. STL does not guarantee the detection of unreasonable errors.
(2) the type of iterator
For STL data structures and algorithms, you can use five iterators. These five types are briefly described below:
Input iterators provides read-only access to data.
Output iterators provides write-only access to data
Forward iterators provides read and write operations and can push iterators forward.
Bidirectional iterators provides read and write operations, and can operate forward and backward.
Random access iterators provides read and write operations and can move randomly through the data.
Although the details of different STL implementations vary, think of the iterator above as a kind of inheritance relationship. In this sense, the following iterator inherits from the above iterator. Because of this inheritance relationship, you can use a Forward iterator as an output or input iterator. Similarly, if an algorithm requires an bidirectional iterator, only that type and random access iterator can be used.
Pointer iterator
As shown on Mini Program below, a pointer is also an iterator. The program also shows one of the main features of STL-it can be used not only for its own class type, but also for any C or C++ type. Listing 1, iterdemo.cpp, shows how to use pointers as iterators for STL's find () algorithm to search for normal arrays.
Table 1. Iterdemo.cpp
# include # include using namespace std; # define SIZE 100 int iarray [SIZE]; int main () {iarray [20] = 50; int* ip = find (iarray, iarray + SIZE, 50); if (ip = = iarray + SIZE) cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.