C++ Deque cend()
C++ Deque cend()
C++ Deque cend()函数返回一个指向过去-最后一个元素的常量迭代器。迭代器可以递增或递减,但不能修改 Deque 的内容。
如果容器为空,则cend()函数返回的内容与cbegin()函数相同。
语法
参数
它不包含任何参数。
返回值
它返回一个常量迭代器
示例1
让我们看一个在 Deque 中包含字符值的简单示例。
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<char> ch={'j','a','v','a','T','p','o','i','n','t'};
deque<char>::const_iterator itr=ch.cbegin();
while(itr!=ch.cend())
{
cout<<*itr;
cout<<" ";
++itr;
}
return 0;
}
输出:
在此示例中,使用cend()函数在整个 Deque 容器中进行迭代,并且while循环将执行,直到'itr'等于ch.cend()为止。
示例2
让我们看一个简单的示例,当 Deque 包含整数值时。
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> deq={100,200,300,400,500};
deque<int>::const_iterator itr=deq.cbegin();
while(itr!=deq.cend())
{
cout<<*itr;
cout<<" ";
++itr;
}
return 0;
}
输出:
在此示例中,使用cend()函数在整个 Deque 容器中进行迭代,并且执行while循环,直到和除非'itr'等于ch.cend()为止。