给定以下代码,如何迭代ProfileCollection类型的对象?
public class ProfileCollection implements Iterable {
private ArrayList<Profile> m_Profiles;
public Iterator<Profile> iterator() {
Iterator<Profile> iprof = m_Profiles.iterator();
return iprof;
}
...
public Profile GetActiveProfile() {
return (Profile)m_Profiles.get(m_ActiveProfile);
}
}
public static void main(String[] args) {
m_PC = new ProfileCollection("profiles.xml");
// properly outputs a profile:
System.out.println(m_PC.GetActiveProfile());
// not actually outputting any profiles:
for(Iterator i = m_PC.iterator();i.hasNext();) {
System.out.println(i.next());
}
// how I actually want this to work, but won't even compile:
for(Profile prof: m_PC) {
System.out.println(prof);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个类TContainer,它是指向TItems类的几个stl集合指针的集合.
我需要创建一个迭代器来遍历我的TContainer类中所有集合中的元素,从而抽象出内部工作的客户端.
这样做有什么好办法?我应该创建一个扩展迭代器的类(如果是这样,我应该扩展什么迭代器类),我应该创建一个迭代器类,它是迭代器的集合吗?
我只需要一个FORWARD_ONLY迭代器.
IE,如果这是我的容器:
typedef std::vector <TItem*> ItemVector;
class TContainer {
std::vector <ItemVector *> m_Items;
};
Run Code Online (Sandbox Code Playgroud)
遍历m_Items成员变量向量中包含的所有项的好迭代器是什么.
我正在尝试实现如下设计:
Touchclass:充当接口,几个类继承自它:
MoveTouchclass
JumpTouchclass
InterfaceTouchclass
然后我想要一个Touch对象列表.然后我希望能够剥离所有MoveTouch对象(而不是其他对象),然后将所有JumpTouch对象分开,等等,从这个大的列表中删除.不幸的是,for (MoveTouch* t in touches)没有做我想做的事; 整个列表中的所有内容都会被执行.