我正在尝试使用"Iterator".remove删除此ArrayList中具有偶数索引号的字符串.
import java.util.*;
import static java.lang.System.out;
class MAIN
{
public static void main(String args[])
{
ArrayList<String> al = new ArrayList<String>();
Iterator i = al.iterator();
int len;
Scanner sc = new Scanner(System.in);
out.printf("Size:");
len = sc.nextInt();
while( len!=0 )
{
al.add( sc.next() );
len -= 1;
}
Object o;
for( len = 0 ; len < al.size() ; len +=1)
{
out.println(al);
if( len%2==0 )
{
o = i.next();
out.println(o);
i.remove();
i.next();
}
}
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我在i.next();得到了"ConcurrentModificationException".怎么了 ?
从我所看到的,如果你有一个iterable并且你在循环中使用iterable.hasNext它将循环遍历数组内的所有元素,但在这里的代码中,它永远不会终止:
//ArrayList<FriendlyBullet> list = . . .;
Iterator<FriendlyBullet> fbi = list.iterator();
while (fbi.hasNext()) { // This loops supposedly infinitely
this.game.list.iterator().next().draw(g2d);
}
Run Code Online (Sandbox Code Playgroud)
我知道列表中没有无限或大量的项目.FriendlyBullet只是我正在使用的一个类.如果我的问题中没有包含对我来说必不可少的内容,请告诉我!我不确定这是我的语法或什么问题,任何帮助非常感谢.
有没有办法获取数组的迭代器?像这样的东西:
class MyCollection implements Iterable<String> {
String[] items;
@Override
public Iterator<String> iterator() {
return items.iterator(); //obviously, it doesn't compile.
}
}
Run Code Online (Sandbox Code Playgroud) 所有,
std::map<int, std::string> addressee;
std::map<int, std::string>::iterator it1, it2;
for( it1 = addressee.begin(); it1 != addressee().end(); it1++ )
{
bool found = false;
for( it2 = it1 + 1; it2 != addressee.end() && !found; it2++ )
{
if( it1->second == it1->second )
{
printf( "Multiple occurences of addressees found" );
found = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
gcc吐出错误:不匹配operator +.
这段代码是我正在尝试做的简化版本.我想我可以使用std :: advance(),但它似乎只是浪费函数调用.
有更好的解决方案吗?
我在使用迭代器时遇到了非常奇怪的错误,我不确定要考虑什么:error: could not convert ‘0’ from ‘int’ to ‘CCarList’
CCarList CRegister::ListCars(const string& name, const string& surname) const {
auto ownerIt = findOwner(name, surname);
if (ownerIt == m_owners.end()) return 0; //Error is on this line
//...
}
Run Code Online (Sandbox Code Playgroud)
findOwner:
vector<TOwner*>::const_iterator CRegister::findOwner(const string& name,
const string& surname) const
{
return lower_bound(m_owners.begin(), m_owners.end(), TOwner(name, surname),
[](const TOwner* a, const TOwner& b){return (*a) < b;});
}
Run Code Online (Sandbox Code Playgroud)
m_owners是vector<TOwner*>(TOwner是struct.)
CCarList:
class CCarList {
vector<TCar*>::iterator m_it;
const vector<TCar*>::iterator m_end;
public:
CCarList(vector<TCar*>::iterator it, const vector<TCar*>::iterator …Run Code Online (Sandbox Code Playgroud) 我正在写一个模板化的类,它涉及使用迭代器.我已经找到了很多关于你如何使用模板来键入迭代器的问题,所以我想知道它为什么还没有用.
#pragma once
#include <iterator>
#include <list>
#include <tuple>
template <class T>
class Quack
{
private:
std::list<T> data;
typename std::list<T>::iterator iter;
public:
Quack();
void insert(T dat);
std::tuple<T, T> poppop();
private:
//error: 'iter' does not name a type
iter binarySearch(T toFind, std::list<T>::iterator min, std::list<T>::iterator max);
};
Run Code Online (Sandbox Code Playgroud)
我也试过typedef typename std::list<T>::iterator iter;,但是引发错误"std :: list :: iterator不是一个类型"
所以鉴于我正在使用typename,我做错了什么?
我正在使用带有参数-std = c ++ 0x的g ++ 4.4.5,如果它是相关的.
首先,为了让您理解这个问题,我将简要解释一下我的项目:
我有一个名为的类Pair,它基本上创建了类型
Pair<String,Double>
Run Code Online (Sandbox Code Playgroud)
并且有一个getFirst()和分别getSecond()返回String和Double值的方法.
然后我有另一个名为的类,Package它基本上包含一个列表Pairs,并实现了Iterable接口,因此我可以通过列表进行迭代:
Package<Pair<String,Double>> package;
List <Pair<String,Double>> list;
Run Code Online (Sandbox Code Playgroud)
我要的是sum在doubles每个Pair使用iterator().
迭代器的定义如下Package:
public Iterator<E> iterator() {
return this.iterator();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两种不同的方法,在两种情况下都产生了:
Exception in thread "main" java.lang.StackOverflowError
at Package.iterator(Package.java:98)
Run Code Online (Sandbox Code Playgroud)
这是第一个:
public static double packageWeight(Package<Pair<String, Double>> package) {
double sum = 0;
Pair<String, Double> pair;
while (package.iterator().hasNext()) {
pair = package.iterator().next();
sum = sum + pair.getSecond();
} …Run Code Online (Sandbox Code Playgroud) 这是我的main方法的代码,应该很明显我试图让迭代器按名称访问元素
#include <iostream>
#include <list>
#include "PC.h"
#include "SunServer.h"
using namespace std;
int main(){
PC mypc1("John", "645.22.55.34", 128, "admin");
PC mypc2("Mike", "645.22.55.34", 128, "admin");
PC mypc3("Rick", "645.22.55.34", 128, "admin");
PC mypc4("Bill", "645.22.55.34", 128, "admin");
list<PC> my_group;
my_group.push_front(mypc1);
my_group.push_front(mypc2);
my_group.push_front(mypc3);
my_group.push_front(mypc4);
list<PC>::iterator curr = my_group.begin();
while (curr != mypc2){
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
现在很明显它不起作用,但有什么我可以做的,这相当于这个?谢谢
字符串迭代器中有一个方法,比如字符串上的find_first_of吗?就像是:
string::iterator it;
string str(" h asdasf ^& saafa");
it = FIND_FIRST_OF("&az^");
std::cout << *it << std::endl;
Run Code Online (Sandbox Code Playgroud)
结果如下:
一个
为清楚起见,我不是问如何遍历字符串向量(这是我的所有搜索都出现了),我想迭代字符串向量中包含的字符串.尝试从向量中的字符串赋值字符串迭代器时,我遇到了链接器错误.
下面的代码引用了一个const向量的字符串.该函数的目的是颠倒字母和元素的顺序,所以如果你传入一个包含{"abc","def","ghi"}的字符串向量,它会重新排序它们并返回一个包含字符串的向量{"ihg","fed","cba"}.应该注意,该函数在头文件中声明为static.当我尝试在for循环中尝试初始化字符串迭代器时,我遇到了问题.我收到链接器错误:
"No matching constructor for initialization of 'std::__1::__wrap_iter<char*>'
Run Code Online (Sandbox Code Playgroud)
我想从矢量迭代器中通过 - >访问rbegin()会起作用但是......我很难过.为什么stringsVectorIterator-> rend()在分配给string :: reverse_iterator rit时会导致链接器错误?
vector<string> StringUtility::reverse(const vector<string> &strings)
{
// Create a vector of strings with the same size vector as the one being passed in.
vector<string> returnValue( strings.size() );
vector<string>::const_reverse_iterator stringsVectorIterator;
size_t returnVectorCounter;
for (stringsVectorIterator = strings.rbegin(), returnVectorCounter = 0;
stringsVectorIterator != strings.rend();
stringsVectorIterator++, returnVectorCounter++)
{
// the problem is in the initialization of the string iterator, it creates
// a linker error.
for (string::reverse_iterator rit = …Run Code Online (Sandbox Code Playgroud)