android c vector用法,C++ vector::cbegin()、vector::cend()用法及代码示例

向量称为动态数组,可以在插入或删除元素时自动更改其大小。此存储由容器维护。

vector::cbegin()

该函数返回用于迭代容器的迭代器。

迭代器指向向量的开头。

迭代器无法修改向量的内容。

用法:

vectorname.cbegin()

参数:

没有参数

返回值:

常数随机访问迭代器指向向量的开头。

异常:

没有例外

下面的程序演示了该功能的工作

// CPP program to illustrate

// use of cbegin()

#include

#include

#include

using namespace std;

int main()

{

vector vec;

// 5 string are inserted

vec.push_back("first");

vec.push_back("second");

vec.push_back("third");

vec.push_back("fourth");

vec.push_back("fifth");

// displaying the contents

cout << "Contents of the vector:" << endl;

for (auto itr = vec.cbegin();

itr != vec.end();

++itr)

cout << *itr << endl;

return 0;

}

输出:

Contents of the vector:

first

second

third

fourth

fifth

vector::cend()

该函数返回用于迭代容器的迭代器。

迭代器指向向量的past-the-end元素。

迭代器无法修改向量的内容。

用法:

vectorname.cend()

参数:

没有参数

返回值:

常量随机访问迭代器指向past-the-end向量的元素。

异常:

没有例外

下面的程序演示了该功能的工作

// CPP programto illustrate

// functioning of cend()

#include

#include

#include

using namespace std;

int main()

{

vector vec;

// 5 string are inserted

vec.push_back("first");

vec.push_back("second");

vec.push_back("third");

vec.push_back("fourth");

vec.push_back("fifth");

// displaying the contents

cout << "Contents of the vector:" << endl;

for (auto itr = vec.cend() - 1;

itr >= vec.begin();

--itr)

cout << *itr << endl;

return 0;

}

输出:

Contents of the vector:

fifth

fourth

third

second

first

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值