The C++ Standard Library 06 The Standard Template Library 标准模板库

本文详细介绍了C++标准模板库(STL),包括其核心组件:容器(如list, array, vector, set, map等)、迭代器、算法。讲解了STL如何通过分离数据和操作来实现高效的数据管理,以及各种容器的特点和使用场景,如vector的随机访问,list和forward_list的插入删除效率。此外,还讨论了STL中不同类型的容器,如顺序容器、关联容器和无序容器的实现和应用。" 130184738,7359303,Python3 入门指南,"['Python', '基础语法', '控制流', '函数', '数据结构', '文件I/O']
摘要由CSDN通过智能技术生成

The C++ Standard Library

The Standard Template Library

本章主要介绍基本容器,包括list, array, forward_list, vector, set, map, multi_map等,并展示相关的小例子,初始化容器,输出元素,插入元素,然后再输出容器。

(01) STL定义与重要性

The heart of the C++ standard library - the part that influenced its overall architecture - is the standard template library (STL). The STL is a generic library that provides solutions to managing collections of data with modern and efficient algorithms.

From the programmer's point of view, the STL provides a bunch of collection classes that meet various needs, together with several algorithms that operate on them. All components of the STL are templates, so they can be used for arbitrary element types.

It provides a framework for supplying other collection classes or algorithms for which existing collection classes and algorithms work.

To use the appropriate kind of collection, you simply define the appropriate container and call the corresponding member functions and algorithms to process the data.

(02)介绍基本概念

This chapter introduces the general concept of the STL and explains the programming techniques need to use it. The first examples show how to use the STL and what to consider while doing so. Chapters 7 through 11 discuss the components of the STL (containers, iterators, function objects, and algorithms) in detail and present several more examples.

The STL is based on the cooperation of various well-structured components, key of which are containers, iterators, and algorithms.

Containers are used to manage collections of objects of a certain kind.

Iterators are used to step through the elements of collections of objects.

The major advantage of iterators is that they offer a small but common interface for any arbitrary container type.

The interface for iterators is almost the same as for ordinary pointers.

Algorithms are used to process the elements of collections.

To give algorithms more flexibility, you can supply certain auxiliary(辅助的) functions called by the algorithms.

(03)Container, Iterator, Algorithm 三者关系

The concept of the STL is based on a separation of data and operations. The data is managed by container classes, and the operations are defined by configurable algorithms. Iterators are the glue between these two components.

(04) STL观念都是新的,有很多想写的。

One fundamental aspect of the STL is that all components work with arbitrary types. As the name "standard template library" indicates, all components are templates for any types, provided that type is able to perfom the require operations. Thus, the STL is a good example of the concept of generic programming.

(05)不同种类的container,直观形象。

(06)三种container

1. Sequence containers are ordered collections in which every elements has a certain position.

The STL contains five predefined sequence container classes: array, vector, deque, list, and forward_list.

2. Associative containers are sorted collections in which the position of an element depends on its value (or key, if it's a key/value pair) due to a certain sorting criterion.

The STL contains four predefined associative container classes: set, multiset, map, and multimap.

3. Unordered (associative) containers are unordered collections in which the position of an element doesn't matter.

The STL contains four predefined unordered container classes: unordered_set, unordered_multiset, unordered_map, and unordered_multimap.

The three container categories introduced here are just logical categories according to the way the order of elements is defined.

Sequence containers are usually implemented as arrays or linked lists.

Associative containers are usually implemented as binary trees.

Unordered containers are usually implemented as has tables.

(07) vector介绍

A vector manages its elements in a dynamic array. It enables random access, which means that you can access each element directly with the corresponding index.

// vector1.cpp

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
    // vector container for integer elements
    vector<int> coll;
    // append elements with value 1 to 6
    for (int i = 0; i <= 6; ++i)
        coll.push_back(i);

    // print all elments followed by a space
    for (int i = 0; i < coll.size(); ++i)
        cout << coll[i] << ' ';
    cout << endl;
}

输出:

0 1 2 3 4 5 6

(08) deque

It is a dynamic array that is implemented so that it can grow in both directions. Thus, inserting elements at the end and at the beginning is fast.

// deque1.cpp
#include <iostream>
#include <deque>
using namespace std;

int main(int argc, char *argv[])
{
    // deque container for floating-point elements
    deque<float> coll;

    // insert elements from 1.1 to 6.6 each at the front
    for (int i = 1; i <= 6; i++)
        coll.push_front(i * 1.1);

    // print all elements followed by a space
    for (int i = 0; i < coll.size(); ++i)
        cout << coll[i] << ' ';

    cout << endl;
}

输出:

6.6 5.5 4.4 3.3 2.2 1.1

(09) array 数组, 数组是最常用的.

// array1.cpp
#include <iostream>
#include <array>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    // array container of 5 string elements
    array<string, 5> coll = {"hello", "world"};

    // print each element with its index on a line
    for (int i = 0; i < coll.size(); ++i) {
        cout << i << ": " << coll[i] << endl;
    }
}

输出:

0: hello
1: world
2:
3:
4:
(10) List 链表

However, Since C++11, two different list containers are provided by the STL: class list<> and class forward_list<>.

A list<> is implemented as a doubly linked list of elements. This means each element in the list has its own segment of memory and refers to its predecessor and its successor.

List don't provide random access.

The advantage of a list is that the insertion or removal of an element is fast at any position. Only the

#include <iostream>
#include <list>

using namespace std;

int main(int argc, char *argv[])
{
    // list container for character elements
    list<char> coll;

    // append elements from 'a' to 'z'
    for (char c = 'a'; c <= 'z'; ++c) {
        coll.push_back(c);
    }

    // print all elements
    // use range-based for loop
    for (auto elem : coll) {
        cout << elem << " ";
    }

    cout << endl;
}

输出;

a b c d e f g h i j k l m n o p q r s t u v w x y z

A direct element access by using operator [] is not provided for lists.

Another way to print all elements before C++11(without using iterators) is to print and remove the first elements in the list.

#include <iostream>
#include <list>

using namespace std;

int main(int argc, char *argv[])
{
    list<char> coll;

    // append elements from 'a' to 'z'
    for (char c = 'a'; c <= 'z'; ++c) {
        coll.push_back(c);
    }

    // print all elements
    // while there are elements
    // print and remove the first elements
    while (!coll.empty()) {
        cout << coll.front() << ' ';
        coll.pop_front();
    }
    cout << endl;
}

输出:

a b c d e f g h i j k l m n o p q r s t u v w x y z

Inside the loop, the front() member function returns the first element.

(11)forward_list

A forward_list<> is implemented as a singly linked list of elements.

#include <iostream>
#include <forward_list>

using namespace std;

int main(int argc, char *argv[])
{
    // create forward-list container for some prime numbers
    forward_list<long> coll = {2, 3, 5, 7, 11, 13, 17};

    // resize two times
    // note: poor performance
    coll.resize(9);
    coll.resize(10, 99);

    // print all elements
    for (auto elem : coll) {
        cout << elem << ' ';
    }

    cout << endl;

  
The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life. Extending the overall content by about 50%, the book now also covers all the new C++11 library components, including Concurrency Fractional arithmetic Clocks and Timers Random numbers and distributions New smart pointers Regular expressions New STL containers, such as arrays, forward lists, and unordered containers New STL algorithms Tuples Type traits and type utilities
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值