编写一个方法removeEvenLength,它将一组字符串作为参数,并从集合中删除所有偶数长度的字符串.
我的解决方案
public static void removeEvenLength(Set<String> set) {
for(String word : set) {
if(word.length() % 2 == 0) {
set.remove(word);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输入:
[foo, buzz, bar, fork, bort, spoon, !, dude]
输出:
ConcurrentModificationException on line 2:
java.util.ConcurrentModificationException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1115)
at java.util.TreeMap$KeyIterator.next(TreeMap.java:1169)
at removeEvenLength (Line 2)
Run Code Online (Sandbox Code Playgroud)
所以我可以通过创建一个来解决它Iterator.但我想知道为什么上面的代码不起作用?
编辑:
迭代器也不起作用:
public static void removeEvenLength(Set<String> set) {
Iterator<String> i = set.iterator();
while(i.hasNext()) {
String word = i.next();
if(word.length() % 2 == 0) {
set.remove(word);
}
}
}
Run Code Online (Sandbox Code Playgroud)
同样的错误.
我正在阅读一本关于数据结构的书,现在正试图实现单链表数据结构.在实现迭代器时,我遇到了重载前缀和后缀增量的这些实现:
iterator &operator++()
{
this->current = this->current->next;
return *this;
}
iterator &operator++(int)
{
iterator old = *this;
++(*this);
return old;
}
Run Code Online (Sandbox Code Playgroud)
我知道第一个是前缀,第二个是后缀,但我不明白为什么重载的后缀增量有不同的代码?如果我这样做会有什么问题?
iterator &operator++(int)
{
this->current = this->current->next;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我在if语句中使用map.find(key)和map.end()函数:
if( p_Repos->returnTypeMap().find(tc[0]) != p_Repos->returnTypeMap().end() )
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我得到一个Microsoft Visual C++运行时库错误,告诉我"表达式:列表迭代器不兼容".tc [0]只是一个字符串,我的地图中的键位置是一个字符串.
但是,它们应该兼容,对吗?
任何帮助是极大的赞赏.
谢谢,汤姆
编辑:根据这里找到的答案:在unordered_map中查找值,我会相信这应该是def.
第二次编辑:
这是returnTypeMap()函数:
std::unordered_map <std::string, std::pair<std::string, std::string>> returnTypeMap()
{
return typeTable;
}
Run Code Online (Sandbox Code Playgroud)
这是我的地图的定义:
std::unordered_map <std::string, std::pair<std::string, std::string>> typeTable;
Run Code Online (Sandbox Code Playgroud) public class Facture {
private Client client = new Client();;
private float Paiement;
private float soustotal;
private float tps;
private float tvq;
private float ttc;
private List<LigneFacture> lignesFac = new ArrayList<LigneFacture>();
public Facture(){
this.Paiement=0;
this.soustotal=0;
this.tps=0;
this.tvq=0;
this.ttc=0;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public float getPaiement() {
return Paiement;
}
public void setPaiement(float Paiement) {
this.Paiement = Paiement;
}
public float getSoustotal() {
return soustotal;
}
public void …Run Code Online (Sandbox Code Playgroud) 我有这个迭代器,
Set<BigFraction> key = knowledgeD.keySet();
TreeSet<BigFraction> sortKey = new TreeSet<BigFraction>();
sortKey.addAll(key);
Iterator<BigFraction> iter = sortKey.iterator();
return iter;
Run Code Online (Sandbox Code Playgroud)
BigFraction只是数据类型,如果它使得使用int或其他东西更容易.
无论如何,当我稍后调用迭代器时
while (iterator().hasNext());
Run Code Online (Sandbox Code Playgroud)
它基本上只是给了我一个无限循环的somesort ...当我打印出iterator()之前我得到了循环
java.util.TreeMap$KeyIterator@53b4b24d
Run Code Online (Sandbox Code Playgroud)
任何想法都被卡住了,
问候,
辛
嘿伙计们这对你们所有人来说都是一个有趣的挑战.我有一个文本文件,我应该逐行处理信息.处理部分是微不足道的,只要我能获得单独的线条.然而,这是挑战:
目前,我最好的解决方案是: 是否有可以逐行遍历文件的C++迭代器? 但我希望有一个更好的,不涉及创建我自己的迭代器类或实现std :: string的代理.
PS这是一个学校作业,这里的挑战是使用标准功能和算法的组合解决问题,但我不知道如何去解决它
我试图对一些itertools针对生成器和列表推导的方法进行基准测试.我的想法是,我想通过过滤基本列表中的一些条目来构建迭代器.
这是我提出的代码(在接受的答案后编辑):
from itertools import ifilter
import collections
import random
import os
from timeit import Timer
os.system('cls')
# define large arrays
listArrays = [xrange(100), xrange(1000), xrange(10000), xrange(100000)]
#Number of element to be filtered out
nb_elem = 100
# Number of times we run the test
nb_rep = 1000
def discard(it):
collections.deque(it, maxlen=0)
def testGenerator(arr, sample):
discard(x for x in sample if x in arr)
def testIterator(arr, sample):
discard(ifilter(sample.__contains__, arr))
def testList(arr, sample):
discard([x for x in sample if …Run Code Online (Sandbox Code Playgroud) 所以我有这个:
std::vector<EnemyInterface*> _activeEnemies;
Run Code Online (Sandbox Code Playgroud)
EnemyInterface看起来像这样:
#include "Ogre.h"
class EnemyInterface{
public:
virtual void update(const Ogre::Real deltaTime) = 0;
virtual void takeDamage(const int amountOfDamage, const int typeOfDamage) = 0;
virtual Ogre::Sphere getWorldBoundingSphere() const = 0;
virtual ~EnemyInterface(){}
};
Run Code Online (Sandbox Code Playgroud)
我创造了一个新的敌人:
// Spikey implements EnemyInterface
activeEnemies.push_back( (EnemyInterface*) &Spikey(_sceneManager, Ogre::Vector3(8,0,0)) );
Run Code Online (Sandbox Code Playgroud)
我想在每个敌人身上调用更新功能,但它崩溃了:
// update enemies
for (std::vector<EnemyInterface*>::iterator it=_activeEnemies.begin(); it!=_activeEnemies.end(); ++it){
(**it).update(timeSinceLastFrame); // Option 1: access violation reading location 0xcccccccc
(*it)->update(timeSinceLastFrame); // Option 2: access violation reading location0xcccccccc
}
Run Code Online (Sandbox Code Playgroud)
我可以在屏幕上看到敌人,但我无法访问它.任何帮助,将不胜感激.
Spikey.h看起来像这样:
#include "EnemyInterface.h"
class Spikey: virtual public …Run Code Online (Sandbox Code Playgroud) 我想写一个通用函数来计算STL容器中元素的总和.我的方式如下(t是一个容器):
template <typename T> double Sum(const T& t){
typename T::reverse_iterator rit = t.rbegin();
double dSum = 0.;
while( rit != t.rend() ){
dSum += (*rit);
++rit;
}
return dSum;
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了很多错误.我想问题是关于我定义迭代器的第二行?不胜感激任何帮助:)
我已经定义了这样一个数组:
ary = [[0,1], [2,3]]
Run Code Online (Sandbox Code Playgroud)
运行以下代码:
ary.reduce(nil) do |a, i, k|
puts "#{a.inspect} #{i.inspect} #{k.inspect}"
end
Run Code Online (Sandbox Code Playgroud)
在每次迭代中,我都期望变量a,i并且k分别保持accumulator(nil),第一个元素和内部数组的第二个元素的值,即我期望这个输出:
nil 0 1
nil 2 3
Run Code Online (Sandbox Code Playgroud)
但结果却是:
nil [0, 1] nil
nil [2, 3] nil
Run Code Online (Sandbox Code Playgroud)
为什么?我怎样才能达到我想要的效果?
此外,为什么下面的代码使用map正如我所期望的那样?
ary.map do |i, k|
puts "#{i.inspect} #{k.inspect}"
end
# Output
# 0 1
# 2 3
Run Code Online (Sandbox Code Playgroud)
有什么不同?
iterator ×10
c++ ×5
java ×3
benchmarking ×1
block ×1
c++11 ×1
containers ×1
file ×1
generator ×1
interface ×1
linked-list ×1
object ×1
python ×1
ruby ×1
set ×1
stdvector ×1
stl ×1
while-loop ×1