STL迭代器的使用、正向、逆向输出双向链表中的所有元素
*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:text.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年6月7日
* 版本号:V1.0
* 问题描述:STL迭代器的使用
* 程序输入:无
* 程序输出:见运行结果
*/
//STL迭代器的使用
#include<iostream>
#include<list> //包含双向链表容器头文件
#include<iterator> //迭代器头文件(可以省略)
using namespace std;
int main()
{
list<int>nums; //整型双向链表,长度为0
nums.insert(nums.begin(),-99); //在链表第一个位置插入-99
nums.insert(nums.begin(),4); //在链表第一个位置插入4
nums.insert(nums.end(),50); //在链表尾插入50
list<int>::const_iterator p1; //p1是整型双向链表的迭代子
cout<<"正向输出双向链表中的所有元素:"<<endl;
for(p1=nums.begin();p1!=nums.end();p1++)
cout<<*p1<<" "; //依次输出链表中所有元素:4,-99,50
cout<<endl;
list<int>::reverse_iterator p2; //p2是整型双向链表的迭代子
p2=nums.rbegin(); //反向迭代指向最后一个元素
cout<<"逆向输出双向链表中的所有元素:"<<endl;
while(p2!=nums.rend()) //当反向迭代不指向第一个元素时
{
cout<<*p2<<" "; //逆向输出链表中的所有元素:50,-99,4
p2++;
}
cout<<endl;
return 0;
}
运行结果:
STL迭代器的使用、正向、逆向输出双向链表中的所有元素的更多相关文章
- 74 使用BitSet输出数组中的重复元素
[本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...
- 【C语言】分别用下标法,地址法和指针法输出数组中的全部元素
#include<stdio.h> int main() { ] = { ,,,, }; int i, * p; printf("下标法:\n"); ; i < ...
- 010-PHP输出数组中第某个元素
<?php $monthName = array(1 => "January", "February", "March",//初 ...
- C++之STL迭代器(iterator)
[摘要]本文是对STL--迭代器(iterator)的讲解,对学习C++编程技术有所帮助,与大家分享. 原文:http://www.cnblogs.com/qunews/p/3761405.html ...
- STL迭代器笔记
STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input output \ ...
- 【C++】:STL迭代器使用---[容器::iterator iter;]
参考文章:http://blog.csdn.net/qq_23100787/article/details/51388163 迭代器这种东西,就是为了使访问简单!! 容器::iterator iter ...
- 一步一步的理解C++STL迭代器
一步一步的理解C++STL迭代器 "指针"对全部C/C++的程序猿来说,一点都不陌生. 在接触到C语言中的malloc函数和C++中的new函数后.我们也知道这两个函数返回的都是一 ...
- C++STL 迭代器
迭代器类别: 输入迭代器(只读迭代器).输出迭代器(只写迭代器).正向迭代器.双向迭代器.随机访问迭代器 逆向遍历 for(vector<int>::reverse_iterator ri ...
- STL迭代器及迭代器失效问题
迭代器失效: 典型的迭代器失效. 首先对于vector而言,添加和删除操作可能使容器的部分或者全部迭代器失效.那为什么迭代器会失效呢?vector元素在内存中是顺序存储,试想:如果当前容器中已经存在了 ...
随机推荐
- apache commons类库的学习
原文地址http://www.tuicool.com/articles/iyEbquE 1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默 ...
- HDU-6708 Windows Of CCPC(打表,递归)
http://acm.hdu.edu.cn/showproblem.php?pid=6708 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- mybatis分页插件PageHelper简单应用
--添加依赖 <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --><depende ...
- 吴裕雄--天生自然 JAVA开发学习:文档注释
/*** 这个类绘制一个条形图 * @author runoob * @version 1.2 */ import java.io.*; /** * 这个类演示了文档注释 * @author Ayan ...
- 吴裕雄--天生自然C语言开发:typedef
#include <stdio.h> #include <string.h> typedef struct Books { ]; ]; ]; int book_id; } Bo ...
- LGOJ2257 YY的GCD
这题美好体验就是卡常 Description link 求 \[\sum^{n}_ {i=1} \sum^m_{j=1} [gcd(i,j)\in prime]\] 其中:\(1\leq n,m \l ...
- html lang="en"
<html lang="en">向搜索引擎表示该页面是html语言,并且语言为英文网站,其"lang"的意思就是“language”,语言的意思,而 ...
- TPO3-2 Depletion of Ogallala Aquifer
Estimates indicate that the aquifer contains enough water to fill Lake Huron, but unfortunately, und ...
- 在CMD中操作mysql数据库出现中文乱码解决方案
百度了一下..有说将cmd字符编码用chcp命令改为65001(utf8字符编码),可这样之后根本无法输入中文,查询出的中问结果依旧乱码 其实,只要保证cmd客户端和MySQL两者编码一致即可. 但现 ...
- 求求你,下次面试别再问我什么是 Spring AOP 和代理了!
https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9403056301388627935% ...