C语言end所有用法,C++ unordered_set cend()用法及代码示例

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值