iterator 相关问题

迭代器是一种面向对象的编程模式,允许遍历集合,不知道实际实现或物理内存中的对象地址。它是四人帮的行为设计模式之一。


制作一个自己保持相反顺序的列表

我的任务是解决以下问题:创建一个将实现 List 的集合 ReverseList。通过使用 for 循环 (for(E e:list)) 迭代 ReverseList 类型的对象列表,我们将...

回答 2 投票 0

为什么在使用 Visual C++ 2022 编译时会出现奇怪的类方法重定义错误?

我正在尝试编写我的向量,使其尽可能类似于STL版本,我使用了这些类型名称。 使用 value_type = 类型; 使用迭代器=向量_迭代器 我正在尝试编写我的向量,使其尽可能类似于 STL 版本,我使用了这些类型名称。 using value_type = Type; using iterator = vector_iterator<value_type>; using const_iterator = const vector_iterator<value_type>; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; 并实现了很多方法,包括以下迭代器相关的方法。 constexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr const_iterator cbegin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept; constexpr const_iterator cend() const noexcept; constexpr reverse_iterator rbegin() noexcept; constexpr const_reverse_iterator rbegin() const noexcept; constexpr const_reverse_iterator crbegin() const noexcept; constexpr reverse_iterator rend() noexcept; constexpr const_reverse_iterator rend() const noexcept; constexpr const_reverse_iterator crend() const noexcept; 就像STL中的标准向量一样,每个begin()、end()、rbegin()和rend()都有两个重载,返回一个非常量迭代器和一个常量迭代器。 cbegin()、cend() 和 crbegin()、crend() 被视为返回 const 迭代器的方法的别名。 每组实现看起来像这样(例如begin()和rend()): // non-const version of begin() template <typename Type, typename Allocator> constexpr typename vector<Type, Allocator>::iterator vector<Type, Allocator>::begin() noexcept { return iterator(ptr_data_); } // const version of begin() template <typename Type, typename Allocator> constexpr typename vector<Type, Allocator>::const_iterator vector<Type, Allocator>::begin() const noexcept { return const_iterator(ptr_data_); } // cbegin() template <typename Type, typename Allocator> constexpr typename vector<Type, Allocator>::const_iterator vector<Type, Allocator>::cbegin() const noexcept { return const_iterator(ptr_data_); } // non-const version of rend() template <typename Type, typename Allocator> constexpr typename vector<Type, Allocator>::reverse_iterator vector<Type, Allocator>::rend() noexcept { return reverse_iterator(begin()); } // const version of rend() template <typename Type, typename Allocator> constexpr typename vector<Type, Allocator>::const_reverse_iterator vector<Type, Allocator>::rend() const noexcept { return const_reverse_iterator(begin()); } // crend() template <typename Type, typename Allocator> constexpr typename vector<Type, Allocator>::const_reverse_iterator vector<Type, Allocator>::crend() const noexcept { return const_reverse_iterator(cbegin()); } 然后,当我尝试运行测试项目时,Visual Studio告诉我,const版本的begin()、const版本的end()、cbegin()和cend()由于重新定义而触发C2373错误. C2373 'clb_container::vector::begin': 重新定义;不同类型修饰符 C2373 'clb_container::vector::cbegin': 重新定义;不同类型修饰符 C2373 'clb_container::vector::end': 重新定义;不同类型修饰符 C2373 'clb_container::vector::cend': 重新定义;不同类型修饰符 这是我的 vector_iterator 的构造函数: template <typename Type> vector_iterator<Type>::vector_iterator(pointer ptr) { iterator_ptr_ = ptr; } template <typename T> vector_iterator<T>::vector_iterator(const vector_iterator& target) { iterator_ptr_ = target.iterator_ptr_; } 我确信我没有两次定义这些方法。 我已尝试以下步骤: 我把上面提到的方法都注释掉了,重新编译了。错误立即消失。 然后我一一取消注释,发现只要const版本的begin()、const版本的end、cbegin()和cend()之一可用,C2373就会再次出现。 我将这些代码片段分别传递给 Copilot 和 Claude-3-Sonnet,他们没有发现明显的重新定义。 最后,我用Visual Studio Installer安装了“C++ Clang tools for Windows under Desktopdevelopment with C++”,并配置了测试项目使用Clang。 C2373消失,程序编译正常。但是,如果我将“平台工具集”更改回默认的“Visual Studio 2022(v143)”,则会再次出现错误。所以我认为这个问题很可能是由 Visual C++ 2022 引起的。 但是,作为我小小的STL的一部分,我希望它能够在大多数编译器下正常编译,而不是因为一些莫名其妙的原因而失败,尤其是在像Visual Studio这样用户众多的平台上,但我仍然找不到真正的问题. constexpr 意味着const... constexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; 是相同的东西,除了返回类型。并且您不能执行仅返回类型不同的重载。

回答 1 投票 0

Rust 中为什么可以将 Range 收集到 HashMap 或 Vec 中?

最近在学习迭代器时遇到了一个问题。我尝试将数字列表转换为 Vec,但是当我将类型更改为 HashMap 时,没有发生错误。例如: 输入 Te...

回答 1 投票 0

如何反向执行 zip 迭代器? - 教堂

如何以相反的顺序执行 zip 迭代器?我需要移动子数组的元素。 我的代码如下: for (x,y) in zip({c..d 按步幅},{a..b 按步幅},){ A1[x]=A1[y]; } 我...

回答 1 投票 0

将 Vec 中与 if 条件匹配的项转换为 NaN [重复]

我有一个带有零值的 Vec,我想将其转换为 NaN。有没有办法通过使用条件语句就地修改 Vec 来做到这一点? 这是我到目前为止所尝试的: 让穆特

回答 1 投票 0

使用迭代器数组读取稀疏信息

我有一个很长的时间范围,我想在这段时间内存储每次神经元“激发”的时间。由于“开火”是一个离散事件,因此可以通过简单地记录...的时间来完成

回答 1 投票 0

在LUA中,如何编写二叉树的迭代器?

在LUA中,如何编写二叉树的迭代器。例如所以我可以做类似的事情: 对于树中的节点:visit() do ... end 我一直在努力,但我能做的最好的就是在树上行走,随身携带......

回答 1 投票 0

如何在 Results 的 Vec 中查找元素的索引,如果发现 Err 则停止?

假设我有一个结果项的集合,例如: 让项目: &[Result<&str, u32>] = &[Ok("foo"), Err(444), Ok("bar")]; 我需要找到第一个

回答 1 投票 0

在 RUST 中实现自定义结构向量的过滤器

我是 Rust 新手,就像昨天刚开始的一样,我正在创建一个待办事项/日历应用程序来学习。我正处于通过结构和实现进行定义的最初阶段。我一直致力于创造一个有趣的...

回答 1 投票 0

在c++中使用自定义分配器,将容器分配给迭代器时,会出现什么问题?

我在编译以下代码时遇到了 GCC 抱怨类型不匹配的问题: #包括 #包括 #包括 #包括

回答 1 投票 0

C++11 中的尾后迭代器失效

关于 C++ 迭代器失效规则的最受欢迎的帖子声称不清楚尾后迭代器(即由 end()、cend()、rend() 和 crend() 返回的迭代器)是否失效一致...

回答 4 投票 0

如何将迭代器传递给目标元素的 lambda 函数?

我想在向量中找到一个元素假设: 向量:向量输入 = {11, 12, 15, 11, 13}; 目标元素:11; 我的代码: auto mylambda = [&target](const int &a) { 返回 a == ta...

回答 1 投票 0

如何使用 map 转换 Rust 中的字符串向量,然后使用 flat_map 将字符串连接到一行中?

我正在为迭代器章节做第二级的沙沙声。 我找到了以下步骤 3 的解决方案,现在我想链接 map 和 flat_map。 // 步骤 3。 // 应用 `capitalize_first`

回答 1 投票 0

std::set begin() 和 std::set 迭代器之间的距离(O(logn))

我需要找到 std::set 中元素的索引。该索引可以可视化为迭代器距开头的距离。 一种方法可以是: for(int i = 0, set::迭代器 it = s....

回答 5 投票 0

在反向迭代器中产生偶数

我希望创建一个迭代器,它以相反的顺序从值中产生偶数。 此代码按升序执行此操作 让 a = (2..6) .step_by(2) .map(|x| x.to_string() + &qu...

回答 1 投票 0

如何在 Pandas 中使用迭代器获取序列的平均值?

我目前正在使用 pandas 来管理 CSV 文件,其中包含地图上船只位置的数据。我有一个 Pandas 数据框,如下所示(已简化): 指数 团体 C_p 1 1 27 2 ...

回答 1 投票 0

编写一个 Rust 函数来修改通用容器元素中的结构成员

是否可以编写一个函数来修改结构成员,以便可以将它与 Vec 和 HashMap::values() 一起使用? 这是我到目前为止所想到的: 使用 std::collections::HashMa...

回答 2 投票 0

如何使用字符串迭代器执行前瞻?

我想尝试熟悉 Rust,所以我正在尝试用 Rust 制作解释器。目前,在“Longer Lexemes”部分中,它有一个 peek 功能,可以将字符放在

回答 1 投票 0

如何在 Rust 中的字符串迭代器中执行前瞻

我想尝试熟悉 Rust,所以我正在尝试用 Rust 制作解释器。目前,在“Longer Lexemes”部分中,它有一个 peek 功能,可以将字符放在

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.