Containers in C++ STL (Standard Template Library)
A container is a holder object that stores a collection of other objects (its elements).
They are implemented as class templates, which allows great flexibility in the types supported as elements.
The container manages the storage space for its elements and provides member functions to access them,
either directly or through iterators (reference objects with similar properties to pointers).
Sequence containers
Sequence containers implement data structures that can be accessed sequentially.
array: Static contiguous array (class template)
vector: Dynamic contiguous array (class template)
deque: Double-ended queue (class template)
forward_list: Singly-linked list (class template)
list: Doubly-linked list (class template)
Associative containers
Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity).
Set: Collection of unique keys, sorted by keys (class template)
Map: Collection of key-value pairs, sorted by keys, keys are unique (class template).
multiset: Collection of keys, sorted by keys (class template)
multimap: Collection of key-value pairs, sorted by keys (class template)
Unordered associative containers
Unordered associative containers implement unsorted (hashed) data structures that can be quickly
searched (O(1) amortized, O(n) worst-case complexity).
unordered_set: Collection of unique keys, hashed by keys. (class template)
unordered_map: Collection of key-value pairs, hashed by keys, keys are unique. (class template)
unordered_multiset: Collection of keys, hashed by keys (class template)
unordered_multimap: Collection of key-value pairs, hashed by keys (class template)
Container adaptors
Container adaptors provide a different interface for sequential containers.
stack: Adapts a container to provide stack (LIFO data structure) (class template).
queue: Adapts a container to provide queue (FIFO data structure) (class template).
priority_queue: Adapts a container to provide priority queue (class template).
iterator
什么是迭代
class template
<iterator>
std::iterator
Iterator is a base class template that can be used to derive iterator classes from it.
It is not an iterator class and does not provide any of the functionality an iterator is expected to have.
This base class only provides some member types, which in fact are not required to be present in any iterator type
(iterator types have no specific member requirements), but they might be useful,
since they define the members needed for the default iterator_traits class template to generate the appropriate
instantiation automatically (and such instantiation is required to be valid for all iterator types).
迭代的函数functions:
advance
back_inserter
begin
distance
end
front_inserter
inserter
make_move_iterator
next
prev
Vector是迭代的例子
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted,
with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that
they can be accessed and traversed using iterators. In vectors, data is inserted at the end.
Inserting at the end takes differential time, as sometimes there may be a need of extending the array.
Removing the last element takes only constant time because no resizing happens.
Inserting and erasing at the beginning or in the middle is linear in time.
// C++ program to illustrate the
// iterators in vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
cout << "\nOutput of cbegin and cend: ";
for (auto i = g1.cbegin(); i != g1.cend(); ++i)
cout << *i << " ";
cout << "\nOutput of rbegin and rend: ";
for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << *ir << " ";
cout << "\nOutput of crbegin and crend : ";
for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir)
cout << *ir << " ";
return 0;
}
其中的 auto怎么替换:std::vector<int>::iterator