C++ Standard Template Library
The C++ STL (Standard Template Library) is a generic collection of class templates and algorithms that allow programmers to easily implement standard data structures like queues, lists, and stacks. The C++ STL provides programmers with the following constructs, grouped into three categories: 序列式容器: C++ Vectors C++ Lists C++ Double-Ended Queues 容器配接器: C++ Stacks C++ Queues C++ Priority Queues 關聯式容器: C++ Maps C++ Multimaps C++ Sets C++ Multisets vector容器 C++ Vectors Vectors contain contiguous elements stored as an array. Accessing members of a vector or appending elements can be done in constant time, whereas locating a specific value or inserting elements into the vector takes linear time. Constructors methods to create vectors Operators assign and compare vectors assign() assign elements to the vector at() returns an element at a specific location back() returns the last element begin() returns an iterator to the first element capacity() the number of elements the vector can hold clear() removes all elements empty() true if the vector is empty end() returns an iterator to the last element erase() removes elements front() returns the first element get_allocator() returns the vector';s allocator insert() insert elements into the vector max_size() returns the maximum number of elements that the vector can hold pop_back() removes the last element push_back() adds an element to the end of the vector rbegin() returns a reverse iterator to the end of the vector rend() returns a reverse iterator to the beginning of the vector reserve() sets the minimum capacity of the vector resize() change the size of the vector size() returns the size of the vector swap() exchange two vectors 特殊的vector:位組 C++ Bitsets give the programmer a set of bits as a data structure. Bitsets can be manipulated by various binary operators such as logical AND, OR, and so on. Constructors create new bitsets Operators compare and assign bitsets any() true if any bits are set count() how many bits are set flip() reverses the bitset none() true if no bits are set reset() clears bits set() sets bits size() number of bits that the bitset can hold test() state of a given bit to_string() string representation of the bitset to_ulong() integer representation of the bitset deque容器 Double-ended queues are like vectors, except that they allow fast insertions and deletions at the beginning (as well as the end) of the container. Constructors create new deques Operators compare and assign deques assign() set the values of the deque at() returns a specific element back() returns the last element begin() returns an iterator to the first element clear() remove all elements empty() true if the deque is empty end() returns an iterator to the end of the queue erase() removes an element front() returns the first element get_allocator() returns the deque';s allocator insert() insert elements into the deque max_size() returns the maximum elements that the deque can hold pop_back() removes the last element pop_front() removes the first element push_back() add an element to the end of the deque push_front() add an element to the front of the deque rbegin() returns a reverse iterator to the end of the deque rend() returns a reverse iterator to the beginning of the deque resize() change the size of the deque size() return the number of elements in the deque swap() swap one deque with another list容器 Lists are sequences of elements stored in a linked list. Compared to vectors, they allow fast insertions and deletions, but slower random access. assign() assign a sequence to the list back() returns the last element begin() returns an iterator to the beginning clear() removes all elements empty() true if the list is empty end() returns an iterator to the end erase() erase an element front() returns the first element get_allocator() returns the list';s allocator insert() insert elements into the list max_size() returns the maximum number of elements the list can hold merge() merge two lists pop_back() removes the last element pop_front() removes the first element push_back() add an element to the end of the list push_front() add an element to the front of the list rbegin() returns a reverse iterator to the beginning of the list remove() removes elements from the list remove_if() removes elements conditionally rend() returns a reverse iterator to the start of the list resize() change the size of the list reverse() reverse the list size() the number the elements in the list sort() sorts the list splice() merge two lists swap() exchange two lists unique() removes duplicate elements stack容器配接器 The C++ Stack is a container adapter that gives the programmer the functionality of a stack -- specifically, a FILO (first-in, last-out) data structure. Operators compare and assign stacks empty() true if the stack is empty pop() removes the top element push() adds an element to the top size() returns the number of elements in the stack top() returns the top element of the stack queue容器配接器 The C++ Queue is a container adapter that gives the programmer a FIFO (first-in, first-out) data structure. back() returns the last element empty() true if the queue is empty front() returns the first element pop() removes the first element push() adds an element to the end of the queue size() returns the number of elements in the queue priority_queue容器配接器 C++ Priority Queues are like queues, but the elements inside the the data structure are ordered by some predicate. empty() true if the priority queue is empty pop() removes the first element push() adds an element size() returns the number of elements in the priority queue top() returns the value of the element with highest priority map容器 C++ Maps are associative containers that contain key/value pairs. begin() returns an iterator to the beginning of the map clear() removes all elements count() returns the number of times an element occurs empty() true if the map is empty end() returns an iterator to the end of the map equal_range() returns iterators to specific items erase() removes an element find() finds an element get_allocator() returns the map';s allocator insert() inserts elements key_comp() returns the function that compares elements lower_bound() returns the first element greater than a certian value max_size() returns the maximum number of elements rbegin() returns a reverse iterator to the end of the map rend() returns a reverse iterator to the beginning of the map size() returns the number of elements in the map swap() swap two maps upper_bound() returns the first element greater than a certian value value_comp() returns the function that compares elements multimap容器 C++ Multimaps are like maps, but they allow duplicate keys. begin() returns an iterator to the first element clear() removes all elements count() number of times an element occurs empty() true if the multimap is empty end() returns an iterator to the last element equal_range() returns iterators to first and last occurances of elements erase() removes elements find() returns an iterator to an element get_allocator() returns the multimap';s allocator insert() inserts elements key_comp() returns the function that compares keys lower_bound() returns an iterator to the first element greater than a certian value max_size() the maximum number of elements the multimap can hold rbegin() returns a reverse iterator to the end of the multimap rend() returns a reverse iterator to the beginning of the multimap size() the number of elements in the multimap swap() swaps two multimaps upper_bound() returns an iterator to the first element greater than a certian value value_comp() returns the function that compares elements set容器 The C++ Set is an associative container that contains an ordered set of objects. begin() returns an iterator to the first element clear() removes all elements count() returns the number of elements empty() true if the set is empty end() returns an iterator to the last element equal_range() returns iterators to the first and last elements that match a certain key erase() removes elements find() finds a given element get_allocator() returns the set';s allocator insert() inserts elements into the set lower_bound() returns an iterator to the first element greater than a certain value key_comp() returns the function that compares keys max_size() the maximum number of elements that the set can hold rbegin() returns a reverse iterator to the end of the set rend() returns a reverse iterator to the beginning of the set size() the number of elements in the set swap() exchanges two sets upper_bound() returns an iterator to the first element greater than a certain value value_comp() returns the function that compares values multiset容器 C++ Multisets are like sets, but they allow duplicate objects. begin() returns an iterator to the first element clear() removes all elements count() returns the number of elements empty() true if the multiset is empty end() returns an iterator to the last element equal_range() returns iterators to the first and last elements matching a specific key erase() removes elements find() returns an iterator to specific elements get_allocator() returns the multiset';s allocator insert() inserts elements into the multiset key_comp() returns the function that compares elements lower_bound() returns an iterator to the first element greater than a certian value max_size() returns the maximum number of elements rbegin() returns a reverse iterator to the end of the multiset rend() returns a reverse iterator to the beginning of the multiset size() number of elements in the multiset swap() swaps two multisets upper_bound() returns an iterator to the first element greater than a certain value value_comp() returns the function that compares elements |