我正在尝试使用BOOST_FOREACH迭代std :: queue.但是那个类中没有迭代器导致我有一个错误:
std::queue<std::string> someList;
BOOST_FOREACH(std::string temp, someList)
{
std::cout << temp;
}
>no matching function for call to begin(...)
>no type named ‘iterator’ in ‘class std::queue<std::basic_string<char> >’
Run Code Online (Sandbox Code Playgroud)
我需要的结构如下:第一个到来,第一个消失.
我在尝试为以下代码示例正确自动完成时遇到了一些问题。我在 Win7 机器上使用 PHPStorm 7。
首先只是一个简单的类。
/**
* Class myObject
*/
class myObject
{
/**
* some method
*/
public function myMethod()
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个集合类,它可以包含先前类的多个实例并实现 ItteratorAggregate 接口。
/**
* Class myCollection
*/
class myCollection implements IteratorAggregate
{
/**
* @var myObject[]
*/
protected $_objects = array();
/**
* @param myObject $object
* @return myCollection
*/
public function add(myObject $object)
{
$this->_objects[] = $object;
return $this;
}
/**
* @return ArrayIterator
*/
public function getIterator()
{
return …Run Code Online (Sandbox Code Playgroud) 假设我已经创建了一个ES6生成器
function *createFibonacciIterator(a = 0, b = 1) {
yield b;
yield *createFib(b, b + a); // <== QUESTION IS ABOUT THIS LINE
}
Run Code Online (Sandbox Code Playgroud)
然后我使用该生成器获得前20个结果
let fibber = createFibonacciIterator();
for (let ii = 0; ii < 20; ii++) {
console.log(fibber.next());
}
Run Code Online (Sandbox Code Playgroud)
如果我离开*了的yield *createFib(b, b + a);线的东西打破,这是有道理的,因为我不希望产生一个迭代器,但实际价值.
*发电机的技术含义是什么?
我知道*Python中的运算符用于解包可迭代对象,例如解包列表。
不过,在实践中,我们也使用*operator来解包迭代器,但我还没有找到解释它的文档。
参见示例:
>>> a = [1,2,3]
>>> print(a)
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
解压可迭代的
>>> print(*a)
1,2,3
Run Code Online (Sandbox Code Playgroud)
解压迭代器
>>> it = iter(a)
>>> print(*it)
1,2,3
Run Code Online (Sandbox Code Playgroud) 我很困惑为什么当我将 L 分配给 iter(x),然后将 next(L) 分配给 x 列表时,迭代代码会起作用。如下所示:
x=[1,2,3,4,5]
>>> L=iter(x)
>>> next(L)
1
>>> next(L)
2
>>> next(L)
3
>>> next(L)
4
>>> next(L)
5
>>> next(L)
Run Code Online (Sandbox Code Playgroud)
但是当我手动编写 next(iter(x)) 来迭代 x 列表时,它不起作用:
next(iter(x))
1
>>> next(iter(x))
1
>>> next(iter(x))
1
Run Code Online (Sandbox Code Playgroud) 我以前从未在 PHP 中使用过生成器,文档中也没有显示返回类型声明的示例。
在 PhpStorm 中,执行此操作时 IDE 中出现错误:
public function getDataIncrementally(): void {
yield from [/* some large set of numbers*/];
}
Run Code Online (Sandbox Code Playgroud)
错误是:
生成器只能声明返回类型为 Generator、Iterator 或 Traversable,或可迭代,不允许使用 void。
我可以看到继承树是Traversable-> Iterator-> Generator。同时,iterable是 PHP 7.1 中引入的一种新的伪类型。
iterable如果我只需要支持 PHP >= 7.1 ,是否适合用于返回类型声明?
我正在尝试解决 C++ 中的一个问题,其中一部分要求我使用rbegin()成员函数从向量中删除元素。但是,每次我编写下面提到的代码时,编译器都会抛出错误。这里有什么问题?
int main() {
int a = 1, b = 2;
vector<int> V = {a, b};
auto it = V.rbegin();
V.erase(it);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用begin()成员函数访问相同的元素,它编译得很好。下面的代码工作正常。
int main() {
int a = 1, b = 2;
vector<int> V = {a, b};
auto it = V.begin()+1;
V.erase(it);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这更像是一个关于 C++ 的好奇问题,是在学习如何查找迭代器引用的索引后提出的。
给定两个向量迭代器,为什么它们可以相减但不能相加?
例如,为什么要编译和运行:
std::vector<int> vect;
vect.begin() - vect.end();
Run Code Online (Sandbox Code Playgroud)
虽然这不会:
std::vector<int> vect;
vect.begin() + vect.end();
Run Code Online (Sandbox Code Playgroud)
问题是更好地了解迭代器的行为方式。对于那些评论和回答的人,谢谢!我需要更多地研究指针算法才能理解这一点。
我认为最有帮助的答案是迭代器就像指针。
减去两个指针来得到它们的距离差是有道理的,就像在数轴上看 1-10 并想要 7 和 3 之间的距离,你从 3 中减去 7 得到 4 的距离。
将 7 和 3 相加得到 10,这并不能帮助我找到它们之间的距离,并且在容器中最终会指向容器边界之外的东西,这没有帮助或有用。
c++ iterator operator-overloading pointer-arithmetic stdvector
我正在观看 Rust 讲座,发现我想到了两种迭代向量的方法。我可以迭代“向量本身”或“iter() 方法”。您能告诉我这里有什么区别吗?
fn call(from: &mut Vec<i32>, to: &mut Vec<i32>) {
for e in from.iter() {
to.push(*e);
}
for e in from {
to.push(*e);
}
}
fn printvec(from: & Vec<i32>) {
for e in from {
print!("{} ", e);
}
println!("");
}
fn main() {
let mut v1 = vec![1,2,3];
let mut v2 = vec![1,2,3];
call(&mut v1, &mut v2);
printvec(&v1);
printvec(&v2);
}
Run Code Online (Sandbox Code Playgroud) 我想返回对集合中拥有的对象的引用(即 a Vec),但我似乎无法获得正确的生命周期。这是我第一次尝试的:
struct StringHolder {
strings: Vec<String>,
i: usize,
}
impl Iterator for StringHolder {
type Item<'a> = &'a String;
fn next(&mut self) -> Option<Self::Item> {
if self.i >= self.strings.len() {
None
} else {
self.i += 1;
Some(&self.strings[self.i])
}
}
}
fn main() {
let sh = StringHolder { strings: vec![], i: 0 };
for string in sh {
println!("{}", string);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,generic associated types are unstable并且lifetimes do not match type in trait …