gpt4 book ai didi

c++ - 如何以链式方式在两个容器上构建迭代器

转载 作者:搜寻专家 更新时间:2023-10-30 23:51:08 24 4
gpt4 key购买 nike

我想提供一个迭代器来迭代 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 对象吗?

  • 如何在从第一个容器跨到第二个容器时实现 operator++?

问题 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com