Containers, iterators, and algorithms in STL----find implementations in vectors

Source: Internet
Author: User

SOURCE http://blog.csdn.net/huangyimin/article/details/6133650

The STL includes containers, iterators, and algorithms:

Containers are used to manage some related data types. Each container has its advantages and disadvantages, and different containers reflect the different needs of the program design. The container itself may be implemented by an array or linked list, or each element in the container has a special key value.

Iterators are used to traverse each element in a dataset. These datasets may be a subset of containers or containers. The main advantage of iterators is that they provide a small and versatile interface for any type of container (note that universal is important). For example, one operation of an iterator interface is to have it iterate through each element of the dataset in turn. This operation is done independently of the internal headquarters structure of the container. The iterator is effective because the container class provides its own iterator type to do "The right thing," and the iterator itself understands the internal structure of the container.

An iterator's interface is almost equivalent to a normal pointer. To increment an iterator, simply call the + + operator. Use the * operator to get the data values referenced by the iterator. Thus iterators can be one as a smart pointer.

The algorithm is used to manipulate the elements in the dataset. For example, they can search, sort, modify data, or other purposes. The algorithm uses iterators, so an algorithm can be used only once for any container, because the interface of an iterator is generic for all types of containers. This is the location of find ()

To give the algorithm more extensibility, it is necessary to provide some subordinate functions that are called by the algorithm. A common algorithm can be used to adapt to very special and complex requirements. You can provide your own search criteria or special actions to bind elements.

The concept of STL is to separate data and operations. The data is managed by the container class, and the operation is defined by the configurable algorithm. The iterator is a clue between these two elements. It allows the interaction of any algorithm and container.

In a sense, the concept of STL is based on object-oriented programming: STL separates data from algorithms rather than binds them. However, the rationale for this is very important: in principle, you can bind any container to any algorithm, and the result is that the STL is very extensible.

One of the standards of STL is that it supports arbitrary data types. The standard template gallery means that all parts are adapted to any type of template. STL is an example of universal programming. Containers and algorithms are common to any type and class.

STL even provides more general-purpose components. Using adapters and function bodies, you can supplement, restrict, and configure algorithms and interfaces for specific needs.

Example of a find vector (found in Baidu), note that find does not belong to the members of the vector, but exists in the algorithm, should be added header file # include <algorithm>:

#include <vector>
#include <algorithm>
#include <iostream>

int main ()
{
using namespace Std;

Vector<int> L;
L.push_back (1);
L.push_back (2);
L.push_back (3);
L.push_back (4);
L.push_back (5);
Vector<int>::iterator result = Find (L.begin (), L.end (), 3); Find 3
if (result = = L.end ())//not found
cout << "No" << Endl;
else//Find
cout << "Yes" << Endl;

}

Containers, iterators, and algorithms in STL----find implementations in vectors

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.