C++ STL一一迭代器相关辅助函数(advance()、distance()、iter_swap())
来源:互联网 发布:各国域名缩写 编辑:程序博客网 时间:2024/05/16 05:10
一、advance()可令迭代器前进
#include <iterator>void advance(InputIterator& pos,Dist n);说明:
(1).使名为pos的Input迭代器步进(或步退)n个元素
(2).对bidirectional迭代器和Random Access迭代器而言,n可为负值,表示向后退。
(3).Dist是个template型别,通常是个整数型别。
(4).advance()并不检查迭代器是否超过序列的end(),所有,对序列尾端调用operator++是一种未定义的操作行为。
示例:
#include <iostream>#include <list>#include <algorithm>using namespace std;int main(){ list<int> coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } list<int>::iterator pos = coll.begin(); // print actual element cout << *pos << endl; // step three elements forward advance (pos, 3); // print actual element cout << *pos << endl; // step one element backward advance (pos, -1); // print actual element cout << *pos << endl;}程序输出结果:
143
二、distance()可处理迭代器之间的距离
#include <iterator>Dist distance(InputIterator pos1, InputIterator pos2);说明:
(1).传回两个Input迭代器pos1,pos2之间的距离
(2).两个敌当前必须指向同一容器
(3).如果不是随机存取迭代器,则从pos1开始往前走必须能够到达pos2,亦即pos2的位置必须与pos1相同或在其后。
(4).回返值Dist的型别由迭代器决定
iterator_traits<InputIterator>::difference_type示例:
#include <iostream>#include <list>#include <algorithm>using namespace std;int main(){ list<int> coll; // insert elements from -3 to 9 for (int i=-3; i<=9; ++i) { coll.push_back(i); } // search element with value 5 list<int>::iterator pos; pos = find (coll.begin(), coll.end(), // range 5); // value if (pos != coll.end()) { // process and print difference from the beginning cout << "difference between beginning and 5: " << distance(coll.begin(),pos) << endl; } else { cout << "5 not found" << endl; }}运行结果:
difference between beginning and 5: 8
#include <algorithm>void iter_swap(ForwardIterator1 pos1,ForwardIterator2 pos2);说明:
(1).交换迭代器pos1和pos2所指的值
(2).迭代器的型别不必相同,但其所指的两个值必须可以相互赋值。
示例:
//print.hpp#include <iostream>/* PRINT_ELEMENTS() * - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */template <class T>inline void PRINT_ELEMENTS (const T& coll, const char* optcstr=""){ typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << ' '; } std::cout << std::endl;}
#include <iostream>#include <list>#include <algorithm>#include "print.hpp"using namespace std;int main(){ list<int> coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } PRINT_ELEMENTS(coll); // swap first and second value iter_swap (coll.begin(), ++coll.begin()); PRINT_ELEMENTS(coll); // swap first and last value iter_swap (coll.begin(), --coll.end()); PRINT_ELEMENTS(coll);}
运行结果:
1 2 3 4 5 6 7 8 92 1 3 4 5 6 7 8 99 1 3 4 5 6 7 8 2
阅读全文
0 0
- C++ STL一一迭代器相关辅助函数(advance()、distance()、iter_swap())
- 使用迭代器辅助函数advance,distance,iter_swap
- stl 学习笔记13 advance()distance() iter_swap()
- C++标准库---advance()&distance()&iter_swap()
- STL通用算法: iter_swap,reverse,reverse_copy,advance算法
- stl的advance和distance的用法
- STL示例10(advance及distance使用)
- STL与泛型编程<九>:迭代器相关辅助函数
- Swift语言:distance、advance函数更改
- 二 迭代器(二)相关辅助函数
- C++ STL一一迭代器
- STL(10)之advance组函数源码
- STL迭代器之辅助迭代器函数
- STL vector 辅助函数
- STL 之swap, iter_swap, swap_ranges
- STL algorihtm算法iter_swap(29)
- STL 之swap, iter_swap, swap_ranges
- 使用stl中的 advance和 distance 方法来进行iterator的加减
- 初学Qt:char*、QbyteArray、QString三种数据类型的转化
- 异常轨道行为,怎么办
- iOS Hybrid框架GPHybrid (WKWebView和UIWebView,涵盖所有常见功能)
- 【第六届蓝桥杯】饮料换购
- leetcode: 45. Jump Game II
- C++ STL一一迭代器相关辅助函数(advance()、distance()、iter_swap())
- PIM各个方式比较表
- pandas练习
- 每天一条Linux语句11/14
- [JavaScript面向对象编程指南]-深入理解JavaScript默认的继承方式——原型链
- UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
- 网优10年
- strlen和sizeof
- 迷宫城堡 HDU