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的算法中运用容器的迭代器时,很可能经常会用到迭代器相应型别(例如迭代器所指物的型别),假设算法中有必要声明一个变量,以"迭代器所指对象的型别"为类型,如何是好,例如我们写一 ...
随机推荐
- assign,copy,retain的区别以及weak和strong的区别
@property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索 ...
- RabbitMQ队列/Redis缓存
一.RabbitMQ队列 RabbitMQ安装(Centos7安装):1.安装依赖:yum install socat (不安装会报错)2.下载rpm包:wget http://www.rabbitm ...
- 所有文本的 attributes 枚举,NSAttributedString
// Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...
- Android开发之旅-获取地理位置的经度和纬度
在Android应用程序中,可以使用LocationManager来获取移动设备所在的地理位置信息.看如下实例:新建android应用程序TestLocation. 1.activity_main.x ...
- requestAnimationFrame 的实验性实践
记得当 requestAnimationFrame 出现时我立马就石更了,就跟初次玩耍 transition 时一样,欣喜若狂... 然后,然后特么的就懵逼了,这明明就是口挖不通的深井呀(如果是我傻, ...
- classpath是什么
作用: 告诉Java执行环境,在哪些目录下可以找到您所要执行的Java程序所需要的类或者包(也就是.class文件) JDK 5.0默认就会到JDK的lib目录下寻找Java程序. 如何配置? 有许多 ...
- IOS中大文件拷贝算法
+ (void)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath { //每次读取数据大小 #define READ_SI ...
- asp.net 5 (mvc 6) 获取网站的物理路径
public static IApplicationEnvironment GetApplication(this RazorPage page) { var ae = page.Context.Re ...
- tomcat 正常启动但不能访问
Eclipse中的Tomcat可以正常启动,不过发布项目之后,无法访问,包括http://localhost:8080/的小猫页面也无法访问到,报404错误.这是因为Eclipse所指定的Server ...
- MySQL之——提示"mysql deamon failed to start"错误的解决方法
网站突然连接不上数据库,于是直接重启了一下服务器.进到cli模式下,执行 service myqsld start 发现还是提示"mysql deamon failed to start&q ...