- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想提供一个迭代器来迭代 2 个容器的内容。
例如,我想隐藏折线的节点存储在两个容器中的事实(出于实现目的):
struct PolyLine {
private:
vector<Point*> m_head_nodes;
vector<Point*> m_tail_nodes;
public:
Iterator begin();
Iterator end();
};
Polyline poly;
cout << "contents of poly:" << endl;
for(Point *p : poly)
cout << p << endl;
迭代器应首先迭代 m_head_nodes,然后迭代 m_tail_nodes。
Q1:您能演示一下如何设置 Iterator 对象吗?
问题 2: 如果第二个容器是 std::list
怎么办?
end()
?我试过如下实现方式,
struct Iterator
{
PolyLine &m_parent;
vector<Point*>::iterator m_it;
Iterator(PolyLine &parent_container)
: m_parent(parent_container) {
}
Iterator& operator++() {
if (m_it == m_parent.m_head_nodes.end())
m_it = m_parent.m_tail_nodes.begin();
else
++m_it;
return *this;
}
Point * operator* () {
return *m_it;
}
};
bool operator== (Iterator &one, Iterator &other) {
return one.m_it == other.m_it;
}
Iterator Polyline::begin() {
Iterator o(this);
o.m_it = m_head_nodes.begin();
return o;
}
Iterator Polyline::end() {
Iterator o(this);
o.m_it = m_tail_nodes.end();
return o;
}
但我并不热衷于保留指向 PolyLine
类的指针。
另外,如果第二个容器是 std::list
,我不知道要保留什么作为 m_it
。
最佳答案
像这样的东西对你有用吗(很明显,这是一个在 10 分钟内解决的问题,所以不要指望委员会用 c++20 或其他东西安装它,哈哈——这只是为了提供一些想法):
#include <iostream>
#include <array>
#include <vector>
#include <deque>
#include <algorithm>
template<typename Pointee0, typename Pointee1, typename It0, typename It1> struct ChainedIter;
template<typename Pointee, typename It0, typename It1>
class ChainedIter<Pointee, Pointee, It0, It1> {
It0 it0, begin0, end0;
It1 it1, begin1, end1;
public:
ChainedIter(It0 begin0, It0 end0, It1 begin1, It1 end1):
it0{begin0}, begin0{begin0}, end0{end0},
it1{begin1}, begin1{begin1}, end1{end1} {}
bool operator==(ChainedIter& rhs) const {
return it0 == rhs.it0 && it1 == rhs.it1;
}
bool operator!=(ChainedIter& rhs) const {
return !(*this == rhs);
}
ChainedIter* operator++() {
if(it0 != end0) ++it0;
else ++it1;
return this;
}
Pointee& operator*() {
if(it0 != end0) return *it0;
else return *it1; // UB if it1 == end1
}
ChainedIter end() {
auto newChainedIter = *this;
newChainedIter.it0 = newChainedIter.end0;
newChainedIter.it1 = newChainedIter.end1;
return newChainedIter;
}
ChainedIter begin() {
auto newChainedIter = *this;
newChainedIter.it0 = newChainedIter.begin0;
newChainedIter.it1 = newChainedIter.begin1;
return newChainedIter;
}
};
template<typename Cont1, typename Cont0>
decltype(auto) createIter(Cont0& cont0, Cont1& cont1) {
auto begin0 = cont0.begin();
auto end0 = cont0.end();
auto begin1 = cont1.begin();
auto end1 = cont1.end();
return ChainedIter<
typename Cont0::value_type,
typename Cont1::value_type,
typename Cont0::iterator,
typename Cont1::iterator> {begin0, end0, begin1, end1};
}
int main() {
std::vector<size_t> v(4, 20);
std::deque<size_t> d(3, 200);
auto iter = createIter(v, d);
std::for_each(iter.begin(), iter.end(), [](const auto& elt) {
std::cout << elt << ' ';
});
std::cout << std::endl;
}
我试图让它与不同的容器类型一起工作,只要它们都在同一个对象上模板化(这作为一个膝跳是有意义的,但也许它可以被增强以允许可转换类型等)。如 main()
所示,它与 vector
以及 deque
一起使用。
我的编译器版本是:
$ g++ --version
g++ (GCC) 9.1.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
所以也许我可以使用 C++17 的模板指南来不依赖于类型推导方便的附加函数,但这本身花了我 10 多分钟来打字,然后又花了一些时间来解决编译错误;另外我敢肯定它还有很多其他对生产来说很糟糕的东西:P
关于c++ - 如何以链式方式在两个容器上构建迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57694713/
一晃五年没写博客了,依旧再C#上耕耘,依旧没有啥建树,现在也不知道.net上还有多少人再使用,在这里分享一些自己觉得写的还算优雅的代码。 对于自己写着完的代码,我特别喜欢链式(来源于jQuer
我正在构建一个吉他和弦查找应用程序。我使用多维数组来表示指板。数组中的每个元素都由具有字符串属性“Note”的 FretSpace 结构表示。为了初始化指板上的音符属性,我传递了要处理的吉他弦的详细信
我在演示代码中使用 setTimeout 函数模拟了 3 个 ajax 调用。我将从一段运行良好的代码开始:所有调用都是并行进行的,我希望所有调用都能成功,否则会出现错误。 var p1 = func
谁能解释一下? a = [2,3,4] b = [5,6,8,9] print(len(a) > 0) print(len(b) > 0) print((len(a) > 0) & len(b) >
我正在处理具有多个子 JSONObject 的 JSONObject。这是我填写内容的方式: myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var)
想象一下这种情况,我有一个需要检查属性的对象。但是,该对象当前可以具有空值。 如何在一个“if”条件下检查这两个条件? 目前,我必须做这样的事情: if (myObject != null) {
我有一个对象集合,称它们为obj。他们有一个 act() 方法。 act() 方法最终会导致 o 上的 event() observable 调用 onComplete。 链接这些的好方法是什么? 即
假设我有一个列表变量 datalist 存储 10,000 个字符串实体。QTableView 只需要显示其中的一些实体。这就是为什么 QTableView 被指定为 QSortFilterProxy
我正在寻找支持链式 MSI 安装的工具(最好不是 InstallShield,而且最好是便宜/免费的)。我有几个小型安装需要能够单独部署,但也需要作为一个组部署,我不想维护多个安装程序。 看起来我需要
在这种情况下,我想迭代集合中除最后 2 个元素之外的所有元素。 假设我采用了一种奇怪的方式,例如 x.Reverse().Skip(2).Reverse()。 每个 LINQ 操作是否会有效地生成一个
对于javascript来说非常陌生,我有两个html数字选择,包括年份,我想将第二个选择与第一个选择链接起来,这样当我在第一个选择中选择年份时(而第二个选择没有选项)首先),第二个选择应包括从所选数
有人可以向我解释一下为什么以下两个链式函数: // returns zero if okay var resetCounter = function (model) { return new Prom
所以我有 2 个 promise 函数。当第一个函数出现错误时,我希望它显示错误消息。当完成或失败时,我希望他们执行一个finally catch all 函数,但由于某种原因它不起作用。我的代码如下
我有一个函数 const func = () => server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyN
(async function() { var a,b; function flush(){ return new Promise(res => {
这个问题已经有答案了: Promise chaining: Use result from previous promise in next then callback [duplicate] (1
这可能不是专业正则表达式理解的问题。唯一重要的是因为我正在运行多个链式替换命令,这些命令会影响文本文件中的某些相同文本。我还想象在替换之前,根据分隔符词(需要多次替换)的使用方式对 txt 文件进行分
我正在尝试构建一组类来定义 OSI 堆栈中协议(protocol)的分层属性...从抽象意义上讲,我只需要从父 python 类继承属性,但我需要能够调用整个类链一次...所以,我正在寻找这样的东西.
我正在努力兑现 promise ,到目前为止我偶然发现了这一点: new Promise((resolve, reject) => { setTimeout(() => { r
我试图理解 promise ,我需要链接它们并装饰来自不同端点的对象宽度数据。 例如: 我的 Node-express 应用程序中有这个 //controller.js export const ge
我是一名优秀的程序员,十分优秀!