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的更多相关文章

  1. 一步一步的理解C++STL迭代器

    一步一步的理解C++STL迭代器 "指针"对全部C/C++的程序猿来说,一点都不陌生. 在接触到C语言中的malloc函数和C++中的new函数后.我们也知道这两个函数返回的都是一 ...

  2. C++ STL迭代器与索引相互转换

    0 前言 C++ STL提供了vector.list等模板容器,极大地方便了编程使用. “遍历”是对容器使用的最常用的操作. 使用迭代器来遍历是最好最高效的遍历方法. 当然,对于有些容器的遍历除了使用 ...

  3. STL迭代器笔记

    STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input         output \       ...

  4. STL 迭代器 iterator const

    STL迭代器很多时候可以当成指针来使用. 但是指针一般可以用const来控制访问. 那迭代器呢. #include <iostream> #include <vector> u ...

  5. STL迭代器的使用、正向、逆向输出双向链表中的所有元素

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  6. 【STL 源码剖析】浅谈 STL 迭代器与 traits 编程技法

    大家好,我是小贺. 点赞再看,养成习惯 文章每周持续更新,可以微信搜索「herongwei」第一时间阅读和催更,本文 GitHub : https://github.com/rongweihe/Mor ...

  7. STL——迭代器与traits编程技法

    一.迭代器 1. 迭代器设计思维——STL关键所在 在<Design Patterns>一书中对iterator模式定义如下:提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素 ...

  8. C++之STL迭代器(iterator)

    [摘要]本文是对STL--迭代器(iterator)的讲解,对学习C++编程技术有所帮助,与大家分享. 原文:http://www.cnblogs.com/qunews/p/3761405.html ...

  9. STL迭代器之一:偏特化

    在stl的算法中运用容器的迭代器时,很可能经常会用到迭代器相应型别(例如迭代器所指物的型别),假设算法中有必要声明一个变量,以"迭代器所指对象的型别"为类型,如何是好,例如我们写一 ...

随机推荐

  1. 剑指offer 面试17题

    面试17题: 题目:打印从1到最大的n位数 题:输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1.2.3一直到最大的3位数999. 解题思路:需要考虑大数问题,这是题目设置的陷 ...

  2. 面向对象高级编程——使用__slots__

    正常情况下,我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Student ...

  3. PL/SQL不能格式化SQL:--PL/SQL Beautifier could not parse text

    PL/SQL sql语句美化器点击没有反应.查看下面提示PL/SQL Beautifier could not parse text.本人此次产生的原因是sql语句语法错误. 工具栏处(如果没有此按钮 ...

  4. LeetCode:搜索二维矩阵【74】

    LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...

  5. 在控制台中实现“单词竞猜”游戏 C# 猜词游戏

    场景 设计规则 a) 这是一个单人玩的游戏. b) 可以分三个级别,分别是高级.中级.低级.不同级别对应的单词系列也不一样.要求一旦玩家选定了要玩的级别,应当先提示它关于此级别最高分是多少,是谁创下的 ...

  6. 【Tech】单点登录系统CAS客户端demo

    服务器端配置请参考: http://www.cnblogs.com/sunshineatnoon/p/4064632.html 工具:myeclipse或者javaee-eclipse 1.启动jav ...

  7. HISAT2的运用

    功能: 用于有参考基因组存在的比对工具(适用于whole-genome, transcriptome, and exome sequencing data) 用法: hisat2-build [opt ...

  8. Kubernetes client-go

    Github地址:https://github.com/kubernetes/client-go 访问kubernetes集群有几下几种方式: 方式 特点 支持者 Kubernetes dashboa ...

  9. 【codevs3031】最富有的人(字典树)

    网址:http://codevs.cn/problem/3031/ 这是蒟蒻写的第一道字典树……听说出市选题的神犇要出字符串,于是就赶紧滚去学了学(然而高精度算字符串算法?) 简单来说,字典树就是把一 ...

  10. streambase一些疑难杂症

    1.webserverReqest控件接收不到换行符\r\n 方案一:这个在streambase7.6.7没有办法处理,只有在streambase7.7.4中才有办法处理,在这个版本中出现了Reque ...