unordered_set::cend()方法是C++ STL中的内置函数,用于返回指向past-the-end元素的const_iterator,该past-the-end元素位于unordered_set容器或其中一个存储桶中。此函数不直接指向容器中的任何元素。它仅用于表示容器的末端或范围的开放末端,如[cbegin,cend)。
注意:const_iterator仅可用于访问元素,而不能修改容器中存在的元素。
用法:
unordered_set_name.cend(n);
参数:此函数接受单个参数n。这是一个可选参数,用于指定存储桶编号。如果未传递此参数,则cend()方法将返回const_iterator,该指针指向容器的最后一个元素之后的位置;如果传递此参数,则cend()方法将返回const_iterator,该指针指向容器中最后一个元素之后的位置unordered_set容器中的特定存储桶。
返回值:此函数返回一个const_iterator,它指向容器中最后一个元素或容器中指定存储区之后的位置。
以下示例程序旨在说明unordered_set::cend()函数:
示例1::
// C++ program to illustrate the
// unordered_set::cend() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// Inserting elements in the std
sampleSet.insert(5);
sampleSet.insert(10);
sampleSet.insert(15);
sampleSet.insert(20);
sampleSet.insert(25);
// Here, the cend() method is used to
// iterate in the range of elements
// present in the unordered_set container
cout << "Elements present in sampleSet are: \n";
for (auto itr = sampleSet.cbegin(); itr != sampleSet.cend();
itr++) {
cout << *itr << endl;
}
return 0;
}
输出:
Elements present in sampleSet are:
25
5
10
15
20
示例2::
// C++ program to illustrate the
// unordered_set::cend() function
#include
#include
using namespace std;
int main()
{
unordered_set sampleSet;
// Inserting elements
sampleSet.insert("Welcome");
sampleSet.insert("To");
sampleSet.insert("GeeksforGeeks");
sampleSet.insert("Computer Science Portal");
sampleSet.insert("For Geeks");
// Here, the cend() method is used to
// iterate in the range of elements
// present in the unordered_set container
cout << "Elements present in sampleSet are: \n";
for (auto itr = sampleSet.cbegin(); itr != sampleSet.cend();
itr++) {
cout << *itr << endl;
}
return 0;
}
输出:
Elements present in sampleSet are:
Welcome
To
GeeksforGeeks
For Geeks
Computer Science Portal