我想迭代一个Set并从集合中删除匹配某些条件的元素.迭代器的文档没有说明在迭代时修改列表.
这可能吗?如果没有,最好的方法是什么?请注意,我只想删除迭代器提供的集合中的元素.
编辑:快速显示这是可能的.我也可以使用以下语法吗?
for(Node n : mySet) {
mySet.remove(n);
}
Run Code Online (Sandbox Code Playgroud) 1)这些生成相同的字节代码吗?
2)如果没有,在某些情况下使用一个是否有任何收益?
// LINQ select statement
return from item in collection
select item.Property;
// foreach in an iterator block
foreach (item in collection)
yield return item.Property;
Run Code Online (Sandbox Code Playgroud) 我正在为迭代器编写一个瘦模板包装器,并在通过结构解除引用操作符时遇到绊脚石,主要是因为指针没有:
#include <vector>
struct mystruct {
int member;
};
template<class iterator>
struct wrap {
typedef typename std::iterator_traits<iterator>::pointer pointer;
iterator internal;
pointer operator->() {return internal.operator->();} //MARK1
};
int main() {
wrap<std::vector<mystruct>::iterator> a;
a->member;
wrap<mystruct*> b;
b->member;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog.cpp: In member function ‘typename std::iterator_traits<_Iter>::pointer wrap<iterator>::operator->() [with iterator = mystruct*]’:
prog.cpp:18: instantiated from here
prog.cpp:11: error: request for member ‘operator->’ in ‘((wrap<mystruct*>*)this)->wrap<mystruct*>::internal’, which is of non-class type ‘mystruct*’
Run Code Online (Sandbox Code Playgroud)
以下方法有效,但我不认为它可以工作.也就是说,如果迭代器的奇怪pointer类型与指向a的指针不同value_type.
pointer operator->() {return &*internal;} //MARK3
Run Code Online (Sandbox Code Playgroud) 我尝试使用boost base64编码器,我找到了一个例子,但我得到了异常
typedef
transform_width< binary_from_base64<std::string::const_iterator>, 8, 6 > it_binary_t
Run Code Online (Sandbox Code Playgroud)
我用过的
std::string b64E(it_binary_t(Encrip.begin()), it_binary_t(Encrip.end()));
Run Code Online (Sandbox Code Playgroud)
我知道了
agentid_coder.exe中0x75b1b9bc处于未处理的异常:Microsoft C++异常:内存位置0x0046ed94处的boost :: archive :: iterators :: dataflow_exception ..
我发现这个解决方法,但我得到了相同的结果
string dec(
it_binary_t(Encrip.begin()),
it_binary_t(Encrip.begin() + Encrip.length() - 1)
);
Run Code Online (Sandbox Code Playgroud)
我正在使用MSVS2008并提升1.38
员工类:
public class Employee {
int empid;
String name;
int age;
public Employee(int empid,String name,int age)
{
this.empid=empid;
this.name=name;
this.age=age;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Run Code Online (Sandbox Code Playgroud)
比较类:
public class Employee_comparator implements Comparator<Employee> {
@Override
public int …Run Code Online (Sandbox Code Playgroud) 正下方,的代码Iterator的++方法:
/** Concatenates this iterator with another.
*
* @param that the other iterator
* @return a new iterator that first yields the values produced by this
* iterator followed by the values produced by iterator `that`.
* @note Reuse: $consumesTwoAndProducesOneIterator
* @usecase def ++(that: => Iterator[A]): Iterator[A]
*/
def ++[B >: A](that: => GenTraversableOnce[B]): Iterator[B] = new Iterator[B] {
// optimize a little bit to prevent n log n behavior.
private var cur : Iterator[B] …Run Code Online (Sandbox Code Playgroud) 在C++中,我试图std::vector::iterator为我的模板化课程.但是,当我编译它时,我得到错误:error C2146: syntax error : missing ';' before identifier 'iterator',error C4430: missing type specifier - int assumed. Note: C++ does not support default-int.我也收到警告warning C4346: 'std::vector<T>::iterator' : dependent name is not a type:
#include <vector>
template<class T> class v1{
typedef std::vector<T>::iterator iterator; // Error here
};
class v2{
typedef std::vector<int>::iterator iterator; // (This works)
};
Run Code Online (Sandbox Code Playgroud)
我甚至试过了
template<typename T> class v1{
typedef std::vector<T>::iterator iterator;
};
Run Code Online (Sandbox Code Playgroud)
和
template<typename T = int> class v1{
typedef std::vector<T>::iterator …Run Code Online (Sandbox Code Playgroud) 我一直在使用高度简洁和直观的C++语法来查找两个已排序的vectors并将结果放在第三个中vector:
vector<bar> a,b,c;
//...
std::set_intersection(a.begin(),a.end(),b.begin(),b.end(),
std::back_inserter(c));
Run Code Online (Sandbox Code Playgroud)
这应该设置c为交集(a,b),假设a和b排序.
但是,如果我只是使用c.begin()(我以为我在某个地方看到了一个例子,这就是我做的原因):
std::set_intersection(a.begin(),a.end(),b.begin(),b.end(),
c.begin());
Run Code Online (Sandbox Code Playgroud)
set_intersection期望OutputIteratorat参数.我认为标准只需要c.begin()返回a forward iterator,我想这可能是也可能不是OutputIterator.
无论如何,代码与c.begin()编译下铿锵.
保证在标准下发生什么?如果这编译,可能发生的事情 - 也就是说,当返回的迭代器c.begin()最终递增超过向量的末尾,并且尝试访问指向的元素时,必须/可能发生什么?在这种情况下,符合标准的实现是否可以无声地扩展向量,因此begin()实际上是一个OutputIterator类似的附加back_inserter?
我问这个主要是为了理解标准如何与迭代器一起工作:真正发生了什么,所以我可以使用STL超越复制和粘贴.
为什么类需要定义__iter__()返回self,以获取类的迭代器?
class MyClass:
def __init__(self):
self.state = 0
def __next__(self):
self.state += 1
if self.state > 4:
raise StopIteration
return self.state
myObj = MyClass()
for i in myObj:
print(i)
Run Code Online (Sandbox Code Playgroud)
控制台日志:
Traceback (most recent call last):
for i in myObj:
TypeError: 'MyClass' object is not iterable
Run Code Online (Sandbox Code Playgroud)
迭代器是具有next(Python 2)或
__next__(Python 3)方法的对象.
添加以下内容的任务:
def __iter__(self):
return self
Run Code Online (Sandbox Code Playgroud)
是返回定义方法的迭代器或类的对象__next__().
但是,当MyClass在myObj = MyClass()行中实例化时,不是返回MyClass对象(定义__next__()方法)的任务,而是由__new__()MyClass实例化了吗?
类定义__next__()方法的对象不是自己的迭代器吗?
我已经研究了问题__iter__方法中返回self的用途是什么?并构建一个基本的Python迭代器,但我仍然无法理解 …
我正在实现一个迭代器,它迭代生成器函数的结果,而不是内存中的数据结构,如矢量或映射.
通过阅读C++ 17§27.2.3 的最终工作草案,输入迭代器(以及扩展名,大多数其他迭代器) 的解引用运算符的返回类型需要一个前向迭代器作为引用.这适用于迭代器迭代的数据结构中存在的项.但是,因为我没有使用数据结构并且在调用取消引用运算符时计算每个项目,所以我没有返回的有效引用; 当操作员返回时,计算的项目将被销毁.为了解决这个问题,我将计算结果存储在迭代器本身并返回对存储结果的引用.这适用于我的用例,但在与任意用户定义类型一起使用时会有自己的问题.
我可以理解允许迭代器返回引用,但为什么这是非变异迭代器的要求?该标准的编写者是否认为生成器和即时转换是迭代器的有效用例?返回值而不是const引用会导致任何实际的伤害吗?
[编辑]:我问的更多的是出于好奇关于为什么标准被写入事情是这样的,因为我已经有一个非常好的解决方法.