1. std::vector
std::vector
is a dynamic array that allows fast random access to elements. To getstd::vector
The header element can be used with an index or iterator.
Access through index:
std::vector<int> vec = {1, 2, 3, 4, 5}; int head = vec[0]; // Get header elements,The first element
Access through iterator:
std::vector<int> vec = {1, 2, 3, 4, 5}; int head = *(); // Get header elements,begin() Returns the iterator pointing to the first element
2. std::list
std::list
is a bidirectional linked list that allows efficient insertion and deletion operations anywhere in the container. To getstd::list
The header element can only be used with iterators.
Access through iterator:
std::list<int> lst = {1, 2, 3, 4, 5}; int head = *(); // Get header elements,begin() Returns the iterator pointing to the first element
std::list does not support accessing elements through indexes because the memory layout of the linked list is not continuous.
3. std::deque
std::deque is a double-ended queue that allows efficient insertion and deletion operations on both ends of a container. Similar to std::vector, std::deque also supports accessing elements through indexes and iterators.
Access via index (available only in compiler implementations that support random access, but not mandatory for the standard):
std::deque<int> deq = {1, 2, 3, 4, 5}; int head = deq[0]; // Get header elements,Note that this is not a standard mandatory behavior
Access through iterator(Recommended method):
std::deque<int> deq = {1, 2, 3, 4, 5}; int head = *(); // Get header elements,begin() Returns the iterator pointing to the first element
4. std::forward_list
std::forward_list
is a one-way linked list that only allows one-way traversal. To getstd::forward_list
The header element can only be used with iterators.
Access through iterator:
std::forward_list<int> flst = {1, 2, 3, 4, 5}; int head = *flst.before_begin().next(); // Get header elements,before_begin() Returns the iterator before pointing to the first element,Then call next()
Note that the iterator of std::forward_list is unidirectional and does not support the --operator, so accessing the header element is slightly special.
5. std::set and std::multiset
std::set and std::multiset are ordered collections based on red and black trees. They do not allow duplicate elements (multiset allows duplicate keys but values still need to be unique). To get the header elements of these containers, you can use an iterator.
Access through the iterator:
std::set<int> s = {1, 2, 3, 4, 5}; int head = *(); // Get header elements,The smallest element
forstd::multiset
, the operation is the same.
6. std::map and std::multimap
std::map
andstd::multimap
Are associated containers based on red and black trees that store key-value pairs. To get the key or value of the header element of these containers, you can use an iterator.
Access keys via iterator:
std::map<int, std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}}; int head_key = ()->first; // Get the key of the header element
Access values through an iterator:
std::string head_value = ()->second; // Get the value of the header element
forstd::multimap
, the operation is the same, but please note that sincemultimap
Repeat keys, so the header element may not be unique.
7. std::unordered_set and std::unordered_multiset
std::unordered_set
andstd::unordered_multiset
Is an unordered collection based on a hash table. They also do not allow duplicate elements (unordered_multiset
Allow duplicate elements). To get the header elements of these containers, you can use an iterator.
Access through iterator:
std::unordered_set<int> us = {1, 2, 3, 4, 5}; int head = *(); // Get header elements,But it is not necessarily the first element of the insertion order
forstd::unordered_multiset
, the operation is the same.
8. std::unordered_map and std::unordered_multimap
std::unordered_map
andstd::unordered_multimap
It is an unordered associative container based on hash table. They store key-value pairs. To get the key or value of the header element of these containers, you can use an iterator.
Access keys via iterator:
std::unordered_map<int, std::string> um = {{1, "one"}, {2, "two"}, {3, "three"}}; int head_key = ()->first; // Get the key of the header element
Access values through an iterator:
std::string head_value = ()->second; // Get the value of the header element
forstd::unordered_multimap
, the operation is the same, but please note that sinceunordered_multimap
Repeat keys, so the header element may not be unique.
9. std::stack
std::stack
is an adapter based on other containers (e.g.std::deque
orstd::vector
) implements the function of the stack. To getstd::stack
The header element (i.e. the top element of the stack) can be usedtop()
method.
passtop()
Method access:
std::stack<int> stk = {1, 2, 3, 4, 5}; int head = (); // Get header elements,That is, the top element of the stack
10. std::queue
std::queue
It is also an adapter based on other containers (e.g.std::deque
) Implement the function of queue. To getstd::queue
The head element (i.e. the head element) can be usedfront()
method.
passfront()
Method access:
std::queue<int> q = {1, 2, 3, 4, 5}; int head = (); // Get header elements,The first element of the team
std::queue
Notop()
Method, this isstd::stack
Unique.
11. std::priority_queue
std::priority_queue
is an adapter based on other containers (e.g.std::vector
) Implement the function of priority queues. To getstd::priority_queue
The header element (i.e. the element with the highest priority) can be usedtop()
method.
passtop()
Method access:
std::priority_queue<int> pq = {1, 2, 3, 4, 5}; int head = (); // Get header elements,That is, the element with the highest priority
Summarize
In the C++ standard library, different containers provide different interfaces to access their head elements. For sequence containers (e.g.std::vector
、std::list
、std::deque
etc.), usually using an index or iterator to access the header element. For associated containers (e.g.std::set
、std::map
and unordered associated containers (e.g.std::unordered_set
、std::unordered_map
etc.), then use an iterator to access the header element. For the stack(std::stack
) and priority queues (std::priority_queue
),usetop()
Method to get the header element. And for queues (std::queue
), then usefront()
Method to get the header element.
Understanding how these containers access header elements is an important step in mastering the C++ standard library, which will help you write and debug C++ programs more efficiently.
The above is the detailed content of the common methods for obtaining head elements in C++ containers. For more information about obtaining head elements in C++ containers, please pay attention to my other related articles!