我正在实现一个具有STL类接口的自定义容器.我必须提供一个常规迭代器和一个const迭代器.两个版本的迭代器的大多数代码都是相同的.我怎样才能避免这种重复?
例如,我的容器类是Foo,我正在实现FooIterator和FooConstIterator.两个迭代器都必须提供operator++()相同的方法.
我的问题类似于如何删除类似的const和非const成员函数之间的代码重复?但是那个问题的答案特定于const和非const方法,尤其是访问器.我没有看到这可能会如何推广到迭代器问题.
我应该FooIterator从FooConstIterator其他非const方法派生并扩展它吗?这要么导致虚拟方法或方法隐藏,这在这里似乎不合适.
也许FooIterator应该包含一个FooConstIterator.虽然这种方法确实减少了实现重复,但它似乎重新引入了许多样板方法定义.
是否有聪明的模板技术从单个定义生成两个迭代器?或许有一种方法 - 颤抖 - 使用预处理器来消除这些几乎相同的类.
我已经尝试查看我的本地STL实现,看看它是如何处理它的.有很多辅助类,我在设计中遇到了麻烦,但看起来功能很简单.
在以前的项目中,我的自定义容器是在标准STL容器之上构建的,所以我不必提供自己的迭代器.在这种情况下,这不是一个选项.
我有一个向量,包含活动或非活动的项目.我希望此向量的大小对于性能问题保持较小,因此我希望从向量中删除已标记为非活动的项目.我在迭代时尝试这样做,但我收到错误"vector iterators incompatible".
vector<Orb>::iterator i = orbsList.begin();
while(i != orbsList.end()) {
bool isActive = (*i).active;
if(!isActive) {
orbsList.erase(i++);
}
else {
// do something with *i
++i;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试对给定路径下的所有文件执行某些操作.我不想事先收集所有的文件名然后用它们做一些事情,所以我尝试了这个:
import os
import stat
def explore(p):
s = ''
list = os.listdir(p)
for a in list:
path = p + '/' + a
stat_info = os.lstat(path )
if stat.S_ISDIR(stat_info.st_mode):
explore(path)
else:
yield path
if __name__ == "__main__":
for x in explore('.'):
print '-->', x
Run Code Online (Sandbox Code Playgroud)
但是这个代码在命中它们时会跳过目录,而不是让它们产生内容.我究竟做错了什么?
出于某种原因,我无法使用附加到我想要使用的对象的函数.我在不起作用的行中添加了注释.作为一个错误,我得到"错误;不允许指向不完整类类型的指针"请帮助
这是dokter.ccp中的代码
int counter = 0;
for (list<Wielrenner*>::iterator it = wielrenners.begin(); it != wielrenners.end(); it++){
Wielrenner* wielrennerOB = *it;
cout << "\nID: " << counter;
cout << "List size: " << persons.size() << endl;
wielrennerOB->print(); // This is not working
counter++;
}
Run Code Online (Sandbox Code Playgroud)
这是wielrenner.h中的代码
#ifndef WIELRENNER_H_
#define WIELRENNER_H_
//#include <fstream>
#include "persoon.h"
#include "Onderzoek.h"
class Wielrenner :
public Persoon
{
public:
Wielrenner(string, string, Adres, string, Datum, Datum, string, int, float, float, float,list<Onderzoek>* );
~Wielrenner(void);
int getLengte() const;
float getGewicht() const;
float …Run Code Online (Sandbox Code Playgroud) 我想编写一个接受迭代器的函数,并返回一些操作的结果.具体来说,我试图迭代a的值HashMap:
use std::collections::HashMap;
fn find_min<'a>(vals: Iterator<Item=&'a u32>) -> Option<&'a u32> {
vals.min()
}
fn main() {
let mut map = HashMap::new();
map.insert("zero", 0u32);
map.insert("one", 1u32);
println!("Min value {:?}", find_min(map.values()));
}
Run Code Online (Sandbox Code Playgroud)
可惜:
error: the `min` method cannot be invoked on a trait object
--> src/main.rs:4:10
|
4 | vals.min()
| ^^^
error[E0277]: the trait bound `std::iter::Iterator<Item=&'a u32> + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:3:17
|
3 | fn find_min<'a>(vals: Iterator<Item = &'a u32>) -> Option<&'a u32> {
| …Run Code Online (Sandbox Code Playgroud) 鉴于以下类,我如何枚举它的属性,即获得类似的输出[station1, station2, station3 ...].我只能看到如何枚举属性的值,即[null, null, null].
class stationGuide {
station1: any;
station2: any;
station3: any;
constructor(){
this.station1 = null;
this.station2 = null;
this.station3 = null;
}
}
Run Code Online (Sandbox Code Playgroud) 我想得到一个istream_iterator样式的迭代器,它将文件的每一行作为字符串而不是每个单词返回.这可能吗?
我有一个字符串,我需要扫描每次出现的"foo"并读取其后的所有文本,直到一秒钟".由于Rust没有,我需要通过字符扫描来迭代它.我该怎么做?contains字符串函数
编辑:Rust &str有一个contains()和find()方法.
我习惯写这样的循环:
for (std::size_t index = 0; index < foo.size(); index++)
{
// Do stuff with foo[index].
}
Run Code Online (Sandbox Code Playgroud)
但是当我在其他代码中看到迭代器循环时,它们看起来像这样:
for (Foo::Iterator iterator = foo.begin(); iterator != foo.end(); iterator++)
{
// Do stuff with *Iterator.
}
Run Code Online (Sandbox Code Playgroud)
我发现这iterator != foo.end()是有争议的.如果iterator增加多于一个也可能是危险的.
使用起来似乎更"正确" iterator < foo.end(),但我从未在实际代码中看到过这种情况.为什么不?
据我所知,任何地方都std::back_inserter可以在STL算法中运行,你可以传递一个std::inserter构造.end()而不是:
std::copy(l.begin(), l.end(), std::back_inserter(dest_list));
std::copy(l.begin(), l.end(), std::inserter(dest_list, dest_list.end()));
Run Code Online (Sandbox Code Playgroud)
并且,不像back_inserter,据我所知,inserter可以为任何STL容器工作!我试了一下成功为std::vector,std::list,std::map,std::unordered_map来这里之前感到惊讶.
我想也许是因为push_back某些结构的速度可能更快insert(.end()),但我不确定......
对于std::list(有道理)似乎并非如此:
// Copying 10,000,000 element-list with std::copy. Did it twice w/ switched order just in case that matters.
Profiling complete (884.666 millis total run-time): inserter(.end())
Profiling complete (643.798 millis total run-time): back_inserter
Profiling complete (644.060 millis total run-time): back_inserter
Profiling complete (623.151 millis total run-time): inserter(.end()) …Run Code Online (Sandbox Code Playgroud)