iterator

C++ Error: Conversion to Non-Scalar Type [closed]

自作多情 提交于 2020-03-18 17:30:08
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I seem to be having a peculiar error in the following code segment (ignore the excess header files and the blank main function, I just wanted to isolate

C++ Error: Conversion to Non-Scalar Type [closed]

谁都会走 提交于 2020-03-18 17:28:27
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I seem to be having a peculiar error in the following code segment (ignore the excess header files and the blank main function, I just wanted to isolate

[19]-容器(集合)_Set

与世无争的帅哥 提交于 2020-03-17 11:03:22
Set 唯一 无序 有相同的元素就不在添加了 底层数据结构是哈希表 存放自定义类型时需要重写hashCode()方法级equals()方法 Hash表原理 /*** (1)调用hashCode()方法计算Hash码值 (2)根据y=k(x)这样的函数计算存储位置 (3)如果位置上没有元素,则将元素存储 (4)如果该位置上有元素,则需调用equals方法比较内容是否相同 */ 使用api大致和map相同 要注意的是 存放自定义类型时需要重写hashCode()方法级equals()方法 package com . lin . map ; import java . util . HashSet ; import java . util . Iterator ; public class TestHashSet { public static void main ( String [ ] args ) { //HashSet底层数组结构使用的是hash表 ,主结构数组, +链表 //创建集合对象 HashSet hs = new HashSet ( ) ; hs . add ( "hello" ) ; System . out . println ( hs . add ( "world" ) ) ; hs . add ( "java" ) ; System . out .

迭代器模式

末鹿安然 提交于 2020-03-17 08:09:10
某厂面试归来,发现自己落伍了!>>> 又称为游标模式, 它提供一种顺序集合、容器对象元素的方法, 而又无须暴露集合内部表示。 特征: 抽离集合对象迭代行为到迭代器中, 提供一致访问接口 属于行为型模式 适用场景: 1、访问一个集合对象的内容而无须暴露它的内部表示 2、为遍历不同的集合结构提供一个统一的访问接口 public interface Iterator< E > { /** * 获取下一个 * @return */ E next () ; /** * 是否存在下一个 * @return */ boolean hasNext () ; } public interface IAggregate< E > { /** * 添加 * @param element * @return */ boolean add ( E element) ; /** * 移除 * @param element * @return */ boolean remove ( E element) ; /** * 迭代器 * @return */ Iterator< E > iterator () ; } public class ConcreteIterator< E > implements Iterator< E > { private List< E > list ; private int

java.util.ConcurrentModificationException 问题处理

末鹿安然 提交于 2020-03-17 04:21:59
java.util.ConcurrentModificationException 问题处理 在对Map集合进行处理时,有时需要对Map集合中的键值对进行过滤删除处理。 例如:对key值进行判断,不满足需求(key值不为“key1”)的需要进行过滤。 1、执行报错的代码如下: public class TraverseMapTestDelete { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); for (String key : map.keySet()) { if ("key1".equals(key)) { map.remove(key); } } map.forEach((key, value) -> System.out.println(key + " : " + value)); } } 报错信息如下: Exception in thread "main" java.util.ConcurrentModificationException at java.util

STL中的map容器的一点总结

守給你的承諾、 提交于 2020-03-16 08:50:48
一、关于map的介绍 map是 STL 的一个容器,和set一样,map也是一种关联式容器。它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,有助于我们处理一对一数据。这里说下map内部数据的组织,map内部是自建一颗红黑树 ( 一种非严格意义上的平衡二叉树 ) ,这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。学习map我们一定要理解什么是 一对一的数据映射?比如:一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int 描述,姓名用字符串描述采用 的 string,于是我们使用的map形式如下:map<int , string> student; 关于map和set底层实现以及效率问题,在另一篇 《STL中set容器的一点总结》 已经说了一些,map和set底层实现都是采用了平衡树来实现的。这里说一下map和set容器的区别。 对于map中的每个节点存储的是一对信息,包括一个键和一个值,各个节点之间的键值不能重复。 对于set中的每个节点存储的是一个信息,只有一个键,但是每个键值也是唯一的。set表示的是集合的概念。 对于map的学习,或者说是对STL中的容器的学习,要知道每种容器的实现原理,每种适合适合解决什么问题的

Class with iter but not next

ぃ、小莉子 提交于 2020-03-16 07:54:52
问题 For the iterator protocol you create both an __iter__ and __next__ method. However, what about the following case: class Item: def __init__(self): self.name = 'James' def __iter__(self): return self Now I can do: >>> i=Item() >>> iter(i) <__main__.Item instance at 0x10bfe6e18> But not: >>> next(i) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: instance has no next() method As far as I'm aware, the definition of iterator/iterable is: Iterable has the method _

java设计模式--迭代器模式

廉价感情. 提交于 2020-03-15 19:53:02
迭代器模式:提供一种方法顺序来访问一个聚合对象中的元素,并且不暴露该对象的内部表示。当我们需要去遍历一个对象时就可以考虑使用迭代器模式。 聚集抽象类 public interface Collection { Iterator createIterator(); } 具体聚集类。 public class ConcreteCollection implements Collection { private List<Object> list = new ArrayList<>(); @Override public Iterator createIterator() { return new ConcreteIterator(this); } //获取集合大小 public int count(){ return list.size(); } //添加元素 public void setList(Object object){ list.add(object); } //获取元素 public Object getList(int index){ return list.get(index); } } 迭代抽象类。 public interface Iterator { //第一个节点 Object first(); //下一个节点 Object next(); //是否遍历完成

【NIO服务器代码-可测试】

蹲街弑〆低调 提交于 2020-03-14 18:14:27
1、目的:了解NIO服务的工作原理 2、代码: /** * @Author: Liu * @Descripition:NIO测试 * @Date; Create in 2020/3/14 16:10 **/ public class NIOServerDemo { private int port = 8080; private Selector selector; private ByteBuffer buffer = ByteBuffer.allocate(1024); //初始化完毕 public NIOServerDemo(int port) { this.port = port; try { ServerSocketChannel server = ServerSocketChannel.open(); server.bind(new InetSocketAddress(this.port)); //设置非阻塞 server.configureBlocking(false); selector = Selector.open(); server.register(selector, SelectionKey.OP_ACCEPT); } catch (IOException e) { e.printStackTrace(); } } public void listen()

hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出

旧时模样 提交于 2020-03-14 11:52:39
基础类,没有重写hashCode()和equals()方法: package niukewang; import java.util.Objects; public class setClass { String a; String b; public setClass(String a, String b) { this.a=a; this.b=b; } } 测试类: package niukewang; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; public class test1 { public static void main(String args[]