C++: STL

The C++ Standard Template Library (STL) - GeeksforGeeksThe Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrahttps://www.geeksforgeeks.org/the-c-standard-template-library-stl/?ref=lbp

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值