一般来说,为了效率和速度目的,缓存一个结束迭代器(特别是STL容器)是个好主意吗?比如在下面的代码中:
std::vector<int> vint;
const std::vector<int>::const_iterator end = vint.end();
std::vector<int>::iterator it = vint.begin();
while (it != end)
{
....
++it;
}
Run Code Online (Sandbox Code Playgroud)
在什么条件下最终价值会失效?从容器中擦除会导致所有 STL容器中的结尾无效还是只有一些?
我有一些看起来像这样的代码:
std::set<int> s1, s2, out;
// ... s1 and s2 are populated ...
std::set_intersection(s1.begin(), s1.end(),
s2.begin(), s2.end(),
std::inserter(out, out.end()));
Run Code Online (Sandbox Code Playgroud)
如果插入到集合中的值紧跟在作为"提示"给出的迭代器之后,我已经读过插入可以在分摊的常量时间内完成.这在运行集合交集时显然是有益的,特别是因为写入的所有内容out已经按排序顺序排列.
我如何保证这种最佳性能?在创建时std::inserter,out是空的,out.begin() == out.end()所以我看不出它是否有任何区别,无论我指定out.begin()还是out.end()作为提示.但是,如果在插入每个元素时解释这一点begin(),那么我似乎不会获得最佳的算法性能.这可以做得更好吗?
// erasing from map
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it(mymap.begin());
// insert some values:
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
mymap['e']=50;
mymap['f']=60;
it=mymap.find('a');
mymap.erase (it); // erasing by iterator
// show content:
for (; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这会产生类似的输出
a => 10
b => 20
c => 30
d => 40
e => 50
f => 60
Run Code Online (Sandbox Code Playgroud)
不 …
可能重复:
Array#each与Array #map
ruby-1.9.2-p180 :006 > ary = ["a", "b"]
=> ["a", "b"]
ruby-1.9.2-p180 :007 > ary.map { |val| p val }
"a"
"b"
=> ["a", "b"]
ruby-1.9.2-p180 :008 > ary.each { |val| p val }
"a"
"b"
=> ["a", "b"]
ruby-1.9.2-p180 :009 > ary.map { |val| val << "2" }
=> ["a2", "b2"]
ruby-1.9.2-p180 :010 > ary.each { |val| val << "2" }
=> ["a22", "b22"]
Run Code Online (Sandbox Code Playgroud) 我收到一个迭代器作为参数,我想迭代两次值.
public void reduce(Pair<String,String> key, Iterator<IntWritable> values,
Context context)
Run Code Online (Sandbox Code Playgroud)
可能吗 ?怎么样 ?签名是由我正在使用的框架(即Hadoop)强加的.
- 编辑 -
最后方法的真实签名reduce是iterable.我被这个wiki页面误导了(这实际上是我发现的wordcount的唯一非弃用(但错误的)示例).
我正在实现本质上是一个容器对象(尽管它确实有一些自己的逻辑).我希望能够遍历此类中的字段中的项目(这只是一个简单的列表).我应该重新实现__iter__和next我的课还是可以接受的返回列表的迭代器,像这样:
class X:
def __init__(self):
self.list = []
def __iter__(self):
return self.list.__iter__()
Run Code Online (Sandbox Code Playgroud)
我不确定这是否会导致任何不良行为.
有没有办法让python中的迭代器指向该项而不增加迭代器本身?例如,如何使用迭代器实现以下内容:
looking_for = iter(when_to_change_the_mode)
for l in listA:
do_something(looking_for.current())
if l == looking_for.current():
next(looking_for)
Run Code Online (Sandbox Code Playgroud) 我如何迭代ruby数组并始终获得两个值,当前和下一个,如:
[1,2,3,4,5,6].pairwise do |a,b|
# a=1, b=2 in first iteration
# a=2, b=3 in second iteration
# a=3, b=4 in third iteration
# ...
# a=5, b=6 in last iteration
end
Run Code Online (Sandbox Code Playgroud)
我的用例:我想测试数组是否已排序,并且通过使用这样的迭代器,我总能比较两个值.
我不是each_slice在这个问题中寻找:将ruby数组转换为连续对的数组
为什么list(next(iter(())) for _ in range(1))返回一个空列表而不是提高StopIteration?
>>> next(iter(()))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> [next(iter(())) for _ in range(1)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> list(next(iter(())) for _ in range(1)) # ?!
[]
Run Code Online (Sandbox Code Playgroud)
使用显式引发的自定义函数也会发生同样的事情StopIteration:
>>> def x():
... raise StopIteration
...
>>> x()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in x
StopIteration …Run Code Online (Sandbox Code Playgroud)