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. C#数组实践

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cont ...

  2. Andorid:日常学习笔记(3)——掌握日志工具的使用

    Andorid:日常学习笔记(3)——掌握日志工具的使用 使用Android的日志工具Log 方法: Android中的日志工具类为Log,这个类提供了如下方法来供我们打印日志: 使用方法: Log. ...

  3. Informatica can bind a LONG value only for insert into a LONG column Oracle

    Informatica实现etl同步表数据信息时 报: Severity Timestamp Node Thread Message Code Message ERROR 2016/8/8 17:32 ...

  4. UI控件之UITabBarController

    UITabBarController:标签栏控制器,继承自UIViewController,用标签栏的形式管理视图控制器,各标签栏对应的视图控制器之间相互独立,互不影响,单击标签栏,显示标签栏对应的视 ...

  5. 转:CWebBrowser2去除边框、滚动条、右键菜单

    http://blog.csdn.net/tangyin025/article/details/8675513 添加CWebBrowser2类 右键项目-〉Add-〉Class...-〉MFC-〉MF ...

  6. ANSI编码——代码页

    详见wiki: http://zh.wikipedia.org/wiki/%E4%BB%A3%E7%A0%81%E9%A1%B5

  7. 每天一个Linux命令(59)wget命令

        wget命令用来从指定的URL下载文件.     (1)用法:     用法:  wget  [参数]  [URL]     (2)功能:     功能:  wget命令用来从指定的URL下载 ...

  8. git全局忽略

    今天在提交一个本地新建的build文件夹内的文件时,sourceTree始终无法找到该文件夹下的任何内容,但是项目中的.gitignore中并没有写/build/配置,于是开始了填坑之路! 首先从表象 ...

  9. php一些常用功能封装

    //二分查找 function bin_sch($array, $low, $high, $k) { if ($low <= $high) { $mid = intval(($low + $hi ...

  10. linux下常用FTP命令 1. 连接ftp服务器[转]

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...