Introduction to STL

Source: Internet
Author: User
Tags abstract require sort

Objective

This article is the author's own use of the STL after some of the views, for want to rely on this article to learn STL, is impossible. Recommend some of the books introduced later.

The concept of STL

In STL, it is divided into three main functions. Collect class, (e.g. vector, list, ...), algorithmic parts (such as sort, find ...), most of which are tools (such as auto_ptr, Make_pair ...), where collect class and The algorithm part is most important.

The STL uses a data type called iterator (someone in Chinese is turned into a generic metric) to connect the Collect class and algorithm parts. In order to achieve the ability to reuse algorithms and collect class. In the STL, these things are further abstracted. So the STL has defined a bunch of abstract behavior called concept. You can imagine that concept is a description of some behavior. As long as one type conforms to the behavior described in concept. Just say this type has this concept.

For example, if there are concept c1,c2. Algorithm A1, A2. Data type D1 has C1 behavior, D2 has c1,c2.

If the A1 requires a C1 type. Then any data type with C1 can be used for A1. In this case, there are D1 and D2.

such as A2 requirements have C1 and C2 type. Only D2 can be used for A2 here.

STL uses abstract behavior to define and implement them to achieve the reuse of algorithms and data types.

Iterator in the STL is an important concept, iterator and C point is very phase, but he is not an indicator (note in some cases iterator is in the C point to implement). He provides functions like point, such as ' + + ' operations that move iterator to the next element, and ' * ' operations represent the element that accesses the iterator currently pointing to (using ' pointing ' is not very good here). Using iterator you can visit the contents of the entire collect.

Give an example to illustrate the function of ITERAOTR.

Vector is one of the many collect classes in the STL. Vector provides a member function such as Begin () and end () to obtain the iterator of the first element in the vector and the next element of the last element. You can use this:

std::vector<int> vtInt;
vtInt.push_back(1);
...
.. // 假设对vtInt 做了很多不同的动做, 如push_back ...
std::vector<int>::iterator itBegin=vtInt.begin();
std::vector<int>::iterator itEnd=vtInt.end();
for( ; itBegin != itEnd ;++itBegin) // 走访itBegin 到 itEnd 不含itEnd 之间的所有element
{
int x=*itBegin; // 取得itBegin 指向的资料
*itBegin = .... ; // 存放资料到itBegin 指向的资料
}

In this example, iterator has similar functions as C point.

All STL algorithms require that the input data be itearot such as the sort algorithm, require two iterator, and then sort the data between the two iterator. By introducing iterator, the sort can be sorted in more things.

Usage examples

I. Use standard output in STL. Say Hello

#include <iostream> // STL 的输入输出包含档. 在STL 中都没有使用.h or .hpp 等副档名
int main(int argc,char* argv[])
{
std::cout << "Hello!! This is STL " << std::endl; // 使用stl 的标准输出, std 是STL 的namespace 所以要这样用std::cout
return 0;
}

II. reading strings from standard input, sorting and then outputting

#include <iostream> // STL 的输入输出包含档
#include <algorithm> // STL 的算法包含档
#include <string> // STL 的字串
#include <vector> // STL 的一个collect class 这是一个像阵列的class
const int d_nReadLine=5;
int main(int argc,char* argv[])
{
std::vector<std::string> vtString; // 宣告一个字串vector
std::string strTemp; // 宣告一个字串
for( int i = 0 ; i < d_nReadLine ; i++) // 读取d_nReadLine 个字串并将结果存到vtString 中
{
std::cin >> strTemp; // 读入字串
vtString.push_back(strTemp); // 将读入的字串存到vtString 的最后面
}
std::sort(vtString.begin(),vtString.end()); // 将vtString 中得资料sort
std::copy(vtString.begin(),vtString.end(),
std::ostream_iterator<std::string>(std::cout,"\n")); // 将vtString 中得资料输出
return 0;
}

Note:

Std::sort and Std::copy are both algorithms provided by STL.

Reference books

Generic program design and STL, Generic programming and the STL. Matthew H. Austern, Houtie/Huang Junyao translation

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.