The Wayback Machine - https://web.archive.org/web/20240912170030/https://www.geeksforgeeks.org/unordered_map-cend-in-c-stl/
Open In App

unordered_map cend in C++ STL

Last Updated : 18 Dec, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The unordered_map::cend() is a built-in function in C++ STL which returns an iterator pointing to the position past the end element in the container or in one of its bucket. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all the elements in the container are covered since the range goes from its begin to its end until invalidated.

There are two variant of this function.
Syntax-1:

unordered_map.cend()

Parameters: This function does not accept any parameter.
Return type: The function returns an iterator to the element past the end of the container.





Output:

5 6
1 2
3 4

Syntax-2:

unordered_map.cend ( size n )

Parameters: This function accepts parameter size n which should be lower than bucket count.
Return type: The function returns an iterator to the one of its bucket count.





Output:

unordered_map bucket contains 
Bucket 0 contains 
Bucket 1 contains 1 2
Bucket 2 contains 
Bucket 3 contains 3 4
Bucket 4 contains 
Bucket 5 contains 5 6
Bucket 6 contains

How is cend() different from end()?
cend() is const version of end(). Similarly cbegin() is a const version of begin(). For example, the following code shows compiler error because we try to modify value in iterator.





Output :

Compilation Error in CPP code :- prog.cpp: In function 'int main()':
prog.cpp:19:20: error: assignment of member 'std::pair::second' in read-only object
         it->second = 10;  // COMPILER ERROR


Similar Reads

vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
list cbegin() and cend() function in C++ STL
The list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.Syntax: list_name.cbegin
2 min read
array::cbegin() and array::cend() in C++ STL
array::cbegin() is a built-in function in C++ STL which returns a const_iterator pointing to the first element in the array. It cannot be used to modify the element in the array which is possible using array::begin(). Syntax: array_name.cbegin() Parameters: The function does not accept any parameters. Return Value: The function returns a const_iter
2 min read
multimap::cbegin() and multimap::cend() in C++ STL
multimap::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.cbegin()Param
3 min read
map cbegin() and cend() function in C++ STL
map::cbegin() is a built-in function in C++ STL that returns a constant iterator referring to the first element in the map container. Since map container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: map_name.cbegin()Parameters: The function d
3 min read
set cbegin() and cend() function in C++ STL
The set::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.cbegin() Parameters: The functi
2 min read
multiset cbegin() and cend() function in C++ STL
The multiset::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator multiset_name.cbegin() Parameters:
3 min read
unordered_set cend() function in C++ STL
The unordered_set::cend() method is a built-in function in C++ STL which is used to return a const_iterator pointing to past-the-end element in the unordered_set container or in one of it's bucket. This function does not directly point to any element in the container. It is only used to denote the end of a container or the open end of a range as in
2 min read
unordered_multiset cend() function in C++ STL
The unordered_multiset::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position immediately after the last element in the container or to the position immediately after the last element in one of its bucket. Syntax: unordered_multiset_name.cend(n) Parameters: The function accepts one parameter. If a param
2 min read
unordered_multimap cend() function in C++ STL
The unordered_multimap::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position after the last element in the container or to the position after the last element in one of its bucket. Syntax: unordered_multimap_name.cend(n) Parameters: The function accepts one parameter. If a parameter is passed, it retur
2 min read
match_results cbegin() add cend() in C++ STL
match_results::cbegin() is an inbuilt function in C++ STL which returns a constant iterator that points to the first match in the match_results object. This iterator cannot modify the contents of the vector. Syntax:smatch_name.cbegin()Parameters: This function does not accept any parameters.Return value: This function returns a constant iterator po
2 min read
forward_list::cend() in C++ STL with Example
forward_list::cend() is a function in C++ STL which returns a constant iterator pointing to the past-the-last element of the forward_list. The iterator returned by the function does not point to any element in the container, but to the position followed by the last element of the forward list container. The iterator returned by the function points
2 min read
unordered_map operator= in C++ STL
The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_map to another unordered_map and unordered_map::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_map as an argument and copies it to an unordered_map. The second version performs a move
2 min read
unordered_map max_size in C++ STL
The unordered_map::max_size is a built in function in C++ STL. It returns maximum number of elements which unordered_map can hold. Maximum number of elements in any container depends upon system and library implementation. Syntax size unordered_map.max_size() Parameters: It does not accept any parameters. Return type: Unsigned integer a container c
2 min read
unordered_map bucket() in C++ STL
The unordered_map::bucket() is a built-in STL function in C++ which returns the bucket number where the element with the key k is located in the map. Syntax: size_type bucket(key) Parameter: The function accepts one mandatory parameter key which specifies the key whose bucket number is to be returned. Return Value: This method returns an unsigned i
1 min read
unordered_map erase in C++ STL
erase function is used to erase elements from the unordered_map. There are three type of erase functions supported by unordered_map : erasing by iterator: It takes an iterator as a parameter and erases the key and value present at that iterator. Syntax unordered_map.erase(const iterator); erasing by key: It takes a key as a parameter and erases the
2 min read
unordered_map end( ) function in C++ STL
The unordered_map::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container in the unordered_map container. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all the elements in the container are covered since th
3 min read
unordered_map max_bucket_count in C++ STL
The unordered_map::max_bucket_count is a built in function in C++ STL. It returns the maximum number of buckets unordered_map container can have. Syntax unordered_map.max_bucket_count() Parameters : It does not accept any parameter. Return Type : Returns maximum number of buckets. Return type is an unsigned integer. Example-1: // C++ program to ill
2 min read
unordered_map operator[] in C++ STL
The std::unordered_map::operator[] is a built in function in C++ STL which returns the reference of value if key matches in the container. If no key is found then it inserts that key into container. Syntax: mapped_type& operator[](key_type&& k); Parameter: It takes parameter as key whose mapped value is accessed. Return type: Returns a
2 min read
unordered_map max_load_factor in C++ STL
The unordered_map::max_load_factor in C++ STL is a built in function which is used to get and set maximum load factor in unordered_map. Load factor is the ratio between number of elements in container and number of buckets. By default unordered_map max_load factor is 1.0. Syntax: There are two type of functions for max_load_factor. float max_load_f
3 min read
unordered_map rehash in C++ STL
The std::unordered_map::rehash() is a built in function C++ STL which sets the number of buckets in container to n or more. Syntax void rehash( size_type s );If s is greater than the current buckets into the container then rehashed is done. The new bucket count can be greater than or equal to n.If s is less than current bucket count then there may
2 min read
unordered_map swap in C++ STL
The std::unordered_map::swap() is a built in function in C++ STL which swaps the elements of a container to an other container. After the call of this function elements of the caller unordered_map will be elements of called unordered_map while elements of called unordered_map will be elements of caller unordered_map. Internally swapping of elements
4 min read
unordered_map load_factor in C++ STL
The unordered_map::load_factor() is a built-in function in C++ STL which returns the current load factor in the unordered_map container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count): load_factor = size / bucket_count The load factor influences the probability of col
3 min read
unordered_map key_eq() function in C++ STL
unordered_map::key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It depends on the key equivalence comparison predicate used by the unordered_map container. The key equivalence comparison is a predicate which takes two arguments and returns a boolean value indicating whether they are to be conside
2 min read
unordered_map hash_function() function in C++ STL
The unordered_map::hash_function() is a built in function in C++ STL which is used to get the hash function. This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it. Syntax: unordered_map_name.hash_function() Parameter: The function does not accept any parameter. Return Value:
2 min read
unordered_map emplace_hint() function in C++ STL
The unordered_map::emplace_hint() is a built-in function in C++ STL which inserts the key and its element in the unordered_map container with a given hint. It effectively increases the container size by one as unordered_map is the container that stores keys with the element value. The hint provided does not affect the position to be entered, it onl
2 min read
unordered_map insert in C++ STL
The unordered_map::insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the unordered_map container. This function increases container size by 1. This function does not insert duplicate entries. There are following variant of this function. All are overloaded functions. Syntax-1: iterator unordered_ma
3 min read
unordered_map clear in C++ STL
unordered_map::clear() function is used to remove all elements from the container. When this function is applied to unordered_map its size becomes zero. Syntax: unordered_map_name.clear() Parameters: This function does not accepts any parameter Return type: This function return nothing. Examples: Input: ump = { {1, 2}, {3, 4}, {5, 6}, {7, 8}} ump.c
2 min read
unordered_map get_allocator in C++ STL
unordered_map::get_allocator() is a built in function in C++ STL which is used to get allocator of container unordered_map. Syntax Allocator_type get_allocator() Parameters: This function does not accept any parameter. Return value: Returns an allocator associated with unordered_map. Below programs explains clearly the unordered_map::get_allocator(
1 min read
unordered_map empty in C++ STL
unordered_map::empty() function is used to check whether container size is zero or not. If container size is zero then it return TRUE otherwise it return FALSE. Syntax: unordered_map_name.empty() Parameters: This function does not accept any parameter Return type: This function returns boolean value TRUE or FALSE. Examples: Input: ump = { {1, 2}, {
2 min read
Practice Tags :