博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
deque学习之元素访问at, operator[], front, back, 和swap操作
阅读量:2193 次
发布时间:2019-05-02

本文共 1787 字,大约阅读时间需要 5 分钟。

本篇学习deque的元素访问和内容交换

at:访问指定的元素,同时进行越界检查

operator[]:访问指定的元素

front:访问第一个元素

back:访问最后一个元素

swap:交换内容

代码实现:

#include 
#include
using namespace std;void getElement(){ deque
deque1 = {11, 13, 16, 18, 19, 12}; for(int &val: deque1) { cout << val << "\t"; } cout << endl; cout << "deque1.front = " << deque1.front() << endl; cout << "deque1.at(2) = " << deque1.at(2) << endl; cout << "deque1[3] ==== " << deque1[3] << endl; cout << "deque1.back == " << deque1.back() << endl; cout << endl; deque
deque2; deque
deque3; deque2.assign(3, 23); deque3.assign({12,14,15,36,67}); for(int &val: deque2) { cout << val << "\t"; } cout << endl; for(int &val: deque3) { cout << val << "\t"; } cout << endl; cout << "deque2.size===" << deque2.size() << endl; cout << "deque3.size===" << deque3.size() << endl; auto it1 = deque2.begin(); auto it2 = deque3.begin(); int &ref1 = deque2.front(); int &ref2 = deque3.front(); cout << "*it1 = " << *it1 << " *it2 = " << *it2 << " ref1 = " << ref1 << " ref2 = " << ref2 << endl; cout << "swap after======" << endl; deque2.swap(deque3); for(int c: deque2) { cout << c << "\t"; } cout << endl; for(int c: deque3) { cout << c << "\t"; } cout << endl; cout << "deque2.size===" << deque2.size() << endl; cout << "deque3.size===" << deque3.size() << endl; cout << "*it1 = " << *it1 << " *it2 = " << *it2 << " ref1 = " << ref1 << " ref2 = " << ref2 << endl; cout << endl;}int main(){ getElement(); cout << endl; cout << "Hello World!" << endl; return 0;}

运行结果:

注意,交换后迭代器与引用保持与原来的元素关联,例如it1指向的元素还是23,ref1引用的元素还23。

参考:

转载地址:http://rfiub.baihongyu.com/

你可能感兴趣的文章
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>
【LEETCODE】205-Isomorphic Strings
查看>>
【LEETCODE】204-Count Primes
查看>>
【LEETCODE】228-Summary Ranges
查看>>
【LEETCODE】27-Remove Element
查看>>
【LEETCODE】66-Plus One
查看>>
【LEETCODE】26-Remove Duplicates from Sorted Array
查看>>
【LEETCODE】118-Pascal's Triangle
查看>>
【LEETCODE】119-Pascal's Triangle II
查看>>
【LEETCODE】88-Merge Sorted Array
查看>>
【LEETCODE】19-Remove Nth Node From End of List
查看>>
【LEETCODE】125-Valid Palindrome
查看>>
【LEETCODE】28-Implement strStr()
查看>>
【LEETCODE】6-ZigZag Conversion
查看>>
【LEETCODE】8-String to Integer (atoi)
查看>>
【LEETCODE】14-Longest Common Prefix
查看>>
【LEETCODE】38-Count and Say
查看>>
【LEETCODE】278-First Bad Version
查看>>