Iterator for STL components (1)

Source: Internet
Author: User

STLAn important feature is the separation of data structures and algorithms. Although this is a simple concept, this separation does make STL very common. For example, because STL's sort () function is completely universal, you can use it to operate almost any data set, including linked lists, containers, and arrays.

Key Points

STL algorithms are provided as template functions. To be different from other components, the STL algorithm in this book is followed by a pair of Circular Arc, such as sort ().

Another important feature of STL is that it is not object-oriented. To be universally available, STL mainly depends on templates rather than encapsulation, inheritance and virtual function polymorphism)-three elements of OOP. You cannot find any obvious class inheritance relationships in STL. This seems to be a kind of regression, but this is exactly the underlying feature that makes STL components have a wide range of versatility. In addition, because STL is based on templates, the use of inline functions makes the generated code short and efficient.

Prompt

Ensure that at least-O optimization should be used to ensure inline extension in compiling programs using STL.

STL components

STL provides a large number of template classes and functions that can be used in OOP and conventional programming. About 50 algorithms of all STL are completely universal and independent from any specific data type. The following sections describe three basic STL components:

1) The iterator provides methods to access objects in the container. For example, you can use an iterator to specify a certain range of objects in the list or vector. The iterator is like a pointer. In fact, the pointer of C ++ is also an iterator. However, the iterator can also be class objects that define operator * () and other methods similar to pointer operators.

2) A container is a data structure, such as list, vector, and deques, provided as a template class. To access the data in the container, you can use the iterator output by the container class.

3) algorithms are template functions used to operate data in containers. For example, STL uses sort () to sort data in a vector and find () to search for objects in a list. Functions are independent of the structure and type of the data they operate on, so they can be used in any data structure from a simple array to a highly complex container.

1. header files

To avoid conflicts with other header files, STL header files no longer use regular. h extensions. To include standard string classes, iterators, and algorithms, use the following indicator:

 
 
  1. #include <string>  
  2. #include <iterator>  
  3. #include <algorithm> 

If you view the STL header file, you can see header files such as iterator. h and stl_iterator.h. Since these names may vary across various STL implementations, you should avoid using these names to reference these header files. To ensure portability, use a file name without the. h suffix.

2. namespace

Your compiler may not recognize namespaces. A namespace is like an envelope that encapsulates a token in another name. The identifier only exists in the namespace, thus avoiding conflict with other identifiers. For example, there may be other libraries and program modules that define the sort () function. To avoid conflicts with the STL sort () algorithm, the STL sort () and other flags are encapsulated in the namespace std. The STL sort () algorithm is compiled into std: sort () to avoid name conflicts.

Although your compiler may not implement namespaces, you can still use them. To use STL, you can insert the following indicator to your source code file, typically after all the # include indicators:

 
 
  1. using namespace std; 


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.