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