vector::cend (c++ 11)

本文详细介绍了 C++ STL 中 std::vector 容器的 cend 成员函数,该函数返回指向容器中最后一个元素之后的常量迭代器。文章包括了 cend 的用法示例、复杂度分析、异常安全性说明等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public member function
<vector>

std::vector::cend

const_iterator cend() const noexcept;
Return const_iterator to end
Returns a const_iterator pointing to the past-the-end element in the container.

const_iterator is an iterator that points to const content. This iterator can be increased and decreased (unless it is itself also const), just like the iterator returned by vector::end, but it cannot be used to modify the contents it points to, even if the vector object is not itself const.

If the container is empty, this function returns the same as vector::cbegin.

The value returned shall not be dereferenced.

Parameters

none

Return Value

const_iterator to the element past the end of the sequence.

Member type const_iterator is a random access iterator type that points to a const element.

// vector::cbegin/cend
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
  vector<int> myvector = {10,20,30,40,50};
  cout << "myvector contains:";
  for (auto it = myvector.cbegin(); it != myvector.cend(); ++it)
    cout << ' ' << *it;
    cout << '\n';

  return 0;
}

Output:

myvector contains: 10 20 30 40 50

Complexity (复杂性)

Constant.

Iterator validity

No changes.   该方法不会对其他迭代器的有效性造成影响。

Data races

The container is accessed.  该容器应该是可访问的。
No contained elements are accessed by the call, but the iterator returned can be used to access them. Concurrently accessing or modifying different elements is safe.

Exception safety

No-throw guarantee: this member function never throws exceptions.
The copy construction or assignment of the returned iterator is also guaranteed to never throw.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值