STL迭代器辅助函数——advance

Advance(i, n) increments the iterator i by the distance n. If n > 0 it is equivalent to executing ++i n times, and if n < 0 it is equivalent to executing --i n times. If n == 0, the call has no effect. 

advance(i, n)使得迭代器i增加一个长度n。如果n>0,那么advance(i, n)等价于执行++i操作n次,如果n<0,那么等价于执行- -i操作n次,如果n=0,那么这个调用没有任何影响。 

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(1),对于其他迭代器,复杂度是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 = 0; i < 7 ; i++, iList++)
        cout << *iList  << "  ";

    // Initialize to the first element"
    iList=List.begin();
    cout << "\n\nAdvance to the 3rd element." << endl;
    advance(iList,2);
    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
                
时间: 2024-08-09 21:33:33

STL迭代器辅助函数——advance的相关文章

STL迭代器笔记

STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input         output \            / forward     |    bidirectional     |        random access 要注意,上面这图表并不是表明它们之间的继承关系:而只是描述了迭代器的种类和接口.处于图表下层的迭代器都是相对于处于图表上层迭代器的扩张集.例如:forward迭代器不但

STL 迭代器 iterator const

STL迭代器很多时候可以当成指针来使用. 但是指针一般可以用const来控制访问. 那迭代器呢. #include <iostream> #include <vector> using namespace std; int main() { vector<int> vecs; vecs.push_back(1); vecs.push_back(3); vecs.push_back(2); //1. 表示改迭代器为const,不能修改.但是指向的值可以改变. 相当于int

C++ STL 迭代器方法 之 advance与prev 方法 浅析

[摘要] 迭代器是STL中重要的一支,prev和distance是其基本方法.distance方法十分简单,就不在此赘述,现主要对prev方法以及其相关方法--advance方法作简要介绍与使用说明,并在文末附上代码示例. [Advance 方法] Advance iterator Advances the iterator it by n element positions. If it is a random-access iterator, the function uses just o

高效STL—迭代器 &amp; 算法

每个标准STL容器提供了四种不容的迭代器:iterator.const_iterator.reverse_iterator和const_reverse_iterator.同时容器的insert和erase的某些形式只接受其中一种. 没有办法从const的迭代器转换为非const的迭代器,不能隐式转换也不能通过const_case转换.但是可以使用advance和distance来进行. Advance(I,distance(I,ci)); //i是一个一般的iterator,ci是一个const

stl迭代器

1.迭代器类型 ·        Input iterators(输入) 提供对数据的只读访问. ·        Output iterators(输出) 提供对数据的只写访问. ·        Forward iterators(正向) 提供读写操作,并能向前推进迭代器. ·        Bidirectional iterators(全向) 提供读写操作,并能向前和向后操作. ·        Random access iterators(随机) 提供读写操作,并能在数据中随机移动.

C++: STL迭代器及迭代器失效问题

转载至:http://blog.csdn.net/wangshihui512/article/details/9791517 迭代器失效: 典型的迭代器失效. 首先对于vector而言,添加和删除操作可能使容器的部分或者全部迭代器失效.那为什么迭代器会失效呢?vector元素在内存中是顺序存储,试想:如果当前容器中已经存在了10个元素,现在又要添加一个元素到容器中,但是内存中紧跟在这10个元素后面没有一个空闲空间,而vector的元素必须顺序存储一边索引访问,所以我们不能在内存中随便找个地方存储

stl迭代器失效

迭代器(iterator)是一个可以对其执行类似指针的操作(如:解除引用(operator*())和递增(operator++()))的对象,我们可以将它理解成为一个指针.但它又不是我们所谓普通的指针,我们可以称之为广义指针,你可以通过sizeof(vector::iterator)来查看,所占内存并不是4个字节.     首先对于vector而言,添加和删除操作可能使容器的部分或者全部迭代器失效.那为什么迭代器会失效呢?vector元素在内存中是顺序存储,试想:如果当前容器中已经存在了10个元

Sword STL迭代器prev,next相关函数

迭代器的头文件中定义了4个实现迭代器模板的函数模板. 1.advance(iterator,num):将迭代器iterator 移动了num个位置 2.distance(iterator1,iterator2):返回两个迭代器之间的元素的个数 3.next(iterator,n):将iterator正向偏移n之后所指向位置的一个迭代器 4.prev(iterator,n):返回iterator反向偏移n之后的所指向的一个迭代器 #include <iostream> #include <

STL迭代器失效总结

转自: http://blog.csdn.net/hackbuteer1/article/details/7734382             http://m.blog.csdn.net/blog/xhu_eternalcc/38355619 迭代器(iterator)是一个可以对其执行类似指针的操作(如:解除引用(operator*())和递增(operator++()))的对象,我们可以将它理 解成为一个指针.但它又不是我们所谓普通的指针,我们可以称之为广义指针,你可以通过sizeof(