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;