STL迭代器辅助函数——advance
Advance(i, n) increments the iterator i by the distance n. If n > it is equivalent to executing ++i n times, and if n < it is equivalent to executing --i n times. If n == , the call has no effect. advance(i, n)使得迭代器i增加一个长度n。如果n>,那么advance(i, n)等价于执行++i操作n次,如果n<,那么等价于执行- -i操作n次,如果n=,那么这个调用没有任何影响。 Defined in the standard header iterator, and in the nonstandard backward-compatibility header iterator.h. 定义在标准头文件<iterator>之中,也存在于向下兼容的(早期的STL)<iterator.h>头文件中。 InputIterator is a model of Input Iterator.
Distance is an integral type that is convertible to InputIterator's distance type. 第一个参数i是 Input Iterator. n的类型应该可以转换成InputIterator's distance类型。 i is nonsingular.
Every iterator between i and i+n (inclusive) is nonsingular.
If InputIterator is a model of input iterator or forward iterator, then n must be nonnegative. If InputIterator is a model of bidirectional iterator or random access iterator, then this precondition does not apply.
对于随即存取迭代器,复杂度是O(),对于其他迭代器,复杂度是O(N)。 因为只有随机访问迭代器提供 + 和-运算符,该库提供两个模板函数、 advance() 和 distance()。
所需的页眉 <iterator> 原型 template<class InIt, class Dist> void advance(InIt& it, Dist n); 说明 高级函数接受两个参数:
初始化: 迭代器前进。
分发: 要通过将迭代器递增的元素数。
高级函数改进迭代器 n 次。该函数的随机访问迭代器类型迭代器时,计算表达式为
iterator += n. 否则,它通过评估执行的每一个增量:
++iterator. 如果迭代器输入或 n 的前向迭代器类型不能为负。 注意: 在原型的类/参数名称可能与中的头文件的版本不匹配。一些已被修改以提高可读性。
示例代码 //////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// <filename> : Advance.cpp
//
// Functions:
//
// advance()
//
// Written by Linda Koontz
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////// /* Compile options needed: /GX
*/
#include <iostream>
#include <string>
#include <list> #if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif #pragma warning (disable:4786) typedef list<string, allocator<string> > STRLIST; void main() {
STRLIST List;
STRLIST::iterator iList;
STRLIST::difference_type dTheDiff; List.push_back("A1");
List.push_back("B2");
List.push_back("C3");
List.push_back("D4");
List.push_back("E5");
List.push_back("F6");
List.push_back("G7"); // Print out the list
iList=List.begin();
cout << "The list is: ";
for (int i = ; i < ; i++, iList++)
cout << *iList << " "; // Initialize to the first element"
iList=List.begin();
cout << "\n\nAdvance to the 3rd element." << endl;
advance(iList,);
cout << "The element is " << *iList << endl;
dTheDiff = distance( List.begin(), iList); } 程序的输出为:
The list is: A1 B2 C3 D4 E5 F6 G7 Advance to the 3rd element. The element is C3
STL迭代器辅助函数——advance的更多相关文章
- 一步一步的理解C++STL迭代器
一步一步的理解C++STL迭代器 "指针"对全部C/C++的程序猿来说,一点都不陌生. 在接触到C语言中的malloc函数和C++中的new函数后.我们也知道这两个函数返回的都是一 ...
- C++ STL迭代器与索引相互转换
0 前言 C++ STL提供了vector.list等模板容器,极大地方便了编程使用. “遍历”是对容器使用的最常用的操作. 使用迭代器来遍历是最好最高效的遍历方法. 当然,对于有些容器的遍历除了使用 ...
- STL迭代器笔记
STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input output \ ...
- STL 迭代器 iterator const
STL迭代器很多时候可以当成指针来使用. 但是指针一般可以用const来控制访问. 那迭代器呢. #include <iostream> #include <vector> u ...
- STL迭代器的使用、正向、逆向输出双向链表中的所有元素
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 【STL 源码剖析】浅谈 STL 迭代器与 traits 编程技法
大家好,我是小贺. 点赞再看,养成习惯 文章每周持续更新,可以微信搜索「herongwei」第一时间阅读和催更,本文 GitHub : https://github.com/rongweihe/Mor ...
- STL——迭代器与traits编程技法
一.迭代器 1. 迭代器设计思维——STL关键所在 在<Design Patterns>一书中对iterator模式定义如下:提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素 ...
- C++之STL迭代器(iterator)
[摘要]本文是对STL--迭代器(iterator)的讲解,对学习C++编程技术有所帮助,与大家分享. 原文:http://www.cnblogs.com/qunews/p/3761405.html ...
- STL迭代器之一:偏特化
在stl的算法中运用容器的迭代器时,很可能经常会用到迭代器相应型别(例如迭代器所指物的型别),假设算法中有必要声明一个变量,以"迭代器所指对象的型别"为类型,如何是好,例如我们写一 ...
随机推荐
- STL之map、set灵活使用
1.LA 5908/UVA1517 Tracking RFIDs 题意:给出s个传感器的位置,以及其感应范围.如果某个方向上有墙,则该方向上感应距离减1.现在有w个墙,给出p个物品的位置,问其能被几个 ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- Linux系统通过console口连接交换机
一.安装minicomUbuntu安装:sudo apt-get install minicom.centos安装:yum install minicom二.配置minicomUbuntu输入:sud ...
- mysql ERROR 1045 (28000): Access denied (using password: YES)
mysql 安装完成后 mysql -u root -p #让输入密码直接回车就能登录 设置mysql的root用户初始密码: mysqladmin -u root password 'root' 解 ...
- 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...
- JSP笔记03——环境搭建(转)
不完全翻译,结合谷歌,一定主观性,还可能有误,原始内容地址:https://www.tutorialspoint.com/jsp/jsp_environment_setup.htm [注释]这篇貌似有 ...
- SQL性能调优策略
1.建立索引 2.避免全表扫描 避免使用is null, is not null,这样写会放弃该字段的索引. 如果会出现这种情况,尽量在设计表的时候设置默认值 比较操作符中!= <>等避免 ...
- KVC和KVO的理解(底层实现原理)
1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...
- iOS清除缓存功能开发
在APP开发中,大量的图片或消息占用系统内存,造成一堆垃圾信息,所以开发清除缓存功能就显得必不可少了. 代码段1:获取文件的大小 - (long long) fileSizeAtPath:(NSStr ...
- 关于 kinect 的开发
1. 参考开发博客:http://www.cnblogs.com/yangecnu/p/Learning-KinectSDK.html