C++ Deque back()函数用于访问deque集合的最后一个元素。
reference back();
它返回对双端队列集合的最后一个元素的引用。
让我们看一个简单的例子
#include <iostream> #include<deque> using namespace std; int main() { deque<int> k={1,2,3,4,5}; cout<<k.back(); return 0; }
输出:
5
在此示例中,back()函数返回双端队列的最后一个元素,即5。
我们来看另一个简单的例子
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-deque-back-function.html
来源:LearnFk无涯教程网
#include <iostream> #include<deque> using namespace std; int main() { deque<int> n; deque<int>::iterator itr; n.push_back(15); while(n.back()!=0) { n.push_back(n.back()-1); } cout<<"Content of deque:"; for(itr=n.begin();itr!=n.end();++itr) cout<<*itr<<" "; return 0; }
输出:
Content of deque:15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)