迭代器

迭代器模式

对着背影说爱祢 提交于 2019-12-01 22:27:13
迭代器模式 迭代器模式又叫做游标(Cursor)模式,其作用是 提供一种方法访问一个容器元素中的各个对象,而又不暴露该对象的内部细节 。 迭代器模式结构 迭代器模式由以下角色组成: 1、迭代器角色 负责定义访问和遍历元素的接口 2、具体迭代器角色 实现迭代器接口,并要记录遍历中的当前位置 3、容器角色 负责提供创建具体迭代器角色的接口 4、具体容器角色 实现创建具体迭代器角色的接口,这个具体迭代器角色与该容器的结构相关 迭代器模式在JDK中的应用及解读 迭代器模式就不自己写例子了,直接使用JDK中的例子。为什么我们要使用迭代器模式,思考一个问题,假如我有一个ArrayList和一个LinkedList: List<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(1); arrayList.add(2); List<Integer> linkedList = new LinkedList<Integer>(); linkedList.add(3); linkedList.add(4); 如何去遍历这两个List相信每个人都很清楚: System.out.println("ArrayList:"); for (int i = 0; i < arrayList.size(); i++) System.out

Debug: 迭代器合法性检测问题

孤人 提交于 2019-12-01 21:04:30
测试代码: 1 for(auto it = v5.cbegin(); it != v5.cend() && !it->empty(); ++it) 2 { 3 cout << *it << " "; 4 } 目的是对迭代器it进行合法性检测。 报错信息: 1 Character_3.cpp:496:51: error: request for member 'empty' in '* it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-><int*, std::vector<int> >()', which is of non-class type 'int' 2 for(auto it = v5.begin(); it != v5.end() && !it->empty(); ++it) Debug最终结果: 1 for(auto it = v5.cbegin(); it != v5.cend() && !isspace(*it); ++it) 2 { 3 cout << *it << " "; 4 } 5 cout << endl; 分析:对于int类型对象,标准库没有定义对应的成员函数empty,因此无法调用对象的empty成员函数,而对于此类报错,应该考虑使用的对象类型与其对应成员函数

牛客 C++刷题day48&49

送分小仙女□ 提交于 2019-12-01 19:54:38
1. vector:随机访问迭代器,复杂度O(1) deque:同上,O(1) map:双向迭代器,不过由于是关联容器,需要通过key访问alue的方法,O(h),h为树的高度 unordered_map:前向迭代器,同上,平摊复杂度O(1),最差O(n),也与散列函数的好坏有关。 string:同vector split是对字符串的分割,而例如10.42这样的数字,编译器自动类型推断中应该是double类型 来源: https://www.cnblogs.com/Tonarinototoro/p/11715501.html

Python3基础-迭代器

房东的猫 提交于 2019-12-01 19:12:17
迭代器介绍 迭代器即用例迭代取值的工具,而迭代式重复反馈过程的活动,其目的通常是为了逼近所需的目标或结果,每一次对过程的重复成为一次”迭代“;而每一次迭代得到的结果会作为下一个迭代的初始值,单纯的重复并不是迭代  可迭代对象 1、语法形式上,内置有__iter__方法的对象都是可迭代对象 2、字符串、列表、元祖、字典、集合、打开的文件都是可迭代对象 str='susu' str_iter=str.__iter__() #字符串 print(str_iter) list=[1,2,'a'] list_iter=list.__iter__() #列表 print(list_iter) dict={'name':'susu','paaswd':'123456'} dict_iter=dict.__iter__() #字典 print(dict_iter) tup=(1,2,3,4) tup_iter=tup.__iter__() #元祖 print(tup_iter) set={1,2,3,4,'yuyu'} set_iter=set.__iter__() #集合 print(set_iter) with open('aa.txt','rb') as f: f_iter=f.__iter__() #打开的文件 print(f_iter) """ 执行结果 <str_iterator

C++ Primer(一)_标准库_顺序容器

萝らか妹 提交于 2019-12-01 17:22:49
目录 顺序容器 顺序容器 选择什么容器根据业务需求, 研读STL剖析了解底层数据结构, 更加清楚各种优势劣势 零碎点 迭代器被设置为 左闭右合 带来的编程假设 begin == end,范围为空 begin != end, 至少一个元素 begin可递增至end 两大类型的容器初始化——同类型容器拷贝,迭代器范围拷贝 前者要求容器类型一致 后者只要求元素可转换 两大类型的容器赋值——=号赋值,assign赋值 前者用于列表或同类型容器 后者用于迭代器,初始化列表,(n,elem)方式;限制顺序容器 swap交换容器内容 除Array,其他容器只是交换数据结构,不涉及元素拷贝插入删除,常数时间 失效内容:除了string容器,其他的都不失效,但是已经转换了容器,而Array则是转换了值 容器之比较——任何容器都支持== !=;除 无序关联 (unordered的关联容器)外,其他都支持< > <= ……各种比较 容器的== !=由元素的==实现;其他由<实现; 然而<也足以推断==情况(交换位置多比一次即可) 顺序容器的操作 失效操作请剖析STL 以下增删改查针对绝大多数顺序容器 增—— 拷贝 emplace区别主要是可用提供 初始化器 而非元素对象 front_list有自己的insert和emplace,只支持下面的front型操作 .push_back(t) .emplace

C++ unordered_map的使用

喜欢而已 提交于 2019-12-01 16:59:30
参考:http://www.cplusplus.com/reference/unordered_map/unordered_map/?kw=unordered_map std::unordered_map(C++11) template < class Key, // unordered_map::key_type class T, // unordered_map::mapped_type class Hash = hash<Key>, // unordered_map::hasher class Pred = equal_to<Key>, // unordered_map::key_equal class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type > class unordered_map; 无序的映射 无序映射是存储键值和映射值组合形成的元素的关联容器,它允许根据键快速检索单个元素。 在unordered_map中,键值通常用于惟一地标识元素,而映射的值是一个对象,其内容与此键相关联。 键和映射值的类型可能不同 。 在内部,unordered_map中的元素没有对键值或映射值以任何特定的顺序排序,但组织成buckets的形式都取决于他们的散列值

146、es6迭代器模拟代码

孤人 提交于 2019-12-01 15:17:40
function createIterator(items) { var i = 0; return { next: function() { var done = (i >= items.length); var value = !done ? items[i++] : undefined; return { done: done, value: value }; } }; } var iterator = createIterator([1, 2, 3]); console.log(iterator.next()); // "{ value: 1, done: false }" console.log(iterator.next()); // "{ value: 2, done: false }" console.log(iterator.next()); // "{ value: 3, done: false }" console.log(iterator.next()); // "{ value: undefined, done: true }" // 之后的所有调用 console.log(iterator.next()); // "{ value: undefined, done: true }" 来源: https://www.cnblogs.com

leetcode-284-顶端迭代器

怎甘沉沦 提交于 2019-12-01 11:55:20
题目描述: 方法:记录前一次的数值 class PeekingIterator: def __init__(self, iterator): """ Initialize your data structure here. :type iterator: Iterator """ self.iterator = iterator self.pre = None def peek(self): """ Returns the next element in the iteration without advancing the iterator. :rtype: int """ if self.pre: return self.pre self.pre = self.iterator.next() return self.pre def next(self): """ :rtype: int """ if self.pre: num = self.pre self.pre = None return num return self.iterator.next() def hasNext(self): """ :rtype: bool """ if self.pre: return True if self.iterator.hasNext(): return True return

HashMap和ConcurrentHashMap的迭代器

好久不见. 提交于 2019-12-01 11:06:39
public class _01_普通map的错误 { public static void useMap(final Map<String,Integer> scores) { scores.put("WU",19); scores.put("zhang",14); try { for(final String key : scores.keySet()) { System.out.println(key + ":" + scores.get(key)); scores.put("liu",12); } }catch (Exception ex) { System.out.println("traverse key fail:" + ex); } try { for (final Integer value : scores.values()) { scores.put("liu", 13); // scores.put("zhao", 13); System.out.println(value); } }catch (Exception ex){ System.out.println("traverse value fail:" + ex); } } public static void main(String[] args) { System.out.println(

Collection接口、Iterator迭代器、增强for遍历

巧了我就是萌 提交于 2019-12-01 09:59:52
Collection接口   创建集合的格式:     Collection<元素类型> 变量名 = new ArrayList<元素类型>();     方式2:Collection 变量名 = new ArrayList();   Collection接口中的方法     添加元素:add("a");     //判断集合中是否包含某元素:contains("b");     删除元素:remove("a");     集合元素长度:size();     清除所有元素:clear();     例: 1 public class Demo02 { 2 public static void main(String[] args) { 3 Collection<String> col=new ArrayList<String>(); 4 col.add("a"); 5 col.add("b"); 6 System.out.println(col); 7 //col.clear(); 8 //System.out.println(col); 9 //判断集合中是否包含某元素 10 boolean flag=col.contains("b"); 11 System.out.println(flag); 12 //删除元素 13 col.remove("a"); 14 System