iterator 相关问题

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

错误 C2794“iterator_category”:不是“std::iterator_traits<_InIt>”的任何直接或间接基类的成员

这是我的代码: #包括 #包括 #包括 类 StrReader { 民众: 类迭代器{ 民众: typedef size_t Difference_type; ...

回答 1 投票 0

为什么迭代器上`.map(…)`的返回类型如此复杂?

我有这个代码: 让 mut myFile = readFile(); 让读者 = BufReader::new(myFile); 让任务= reader.lines() .map(|line| line.expect("无法读取行")); 当我将鼠标悬停在“...

回答 1 投票 0

对 C++ 中类似指针迭代器的类似迭代器概念定义进行故障排除

我是 C++20 的新手。我一直在完善 C++ 概念 IteratorLike,旨在封装迭代器的行为。该概念检查元素访问、迭代器前进和序列结束

回答 1 投票 0

如何通过自可变对结构上的向量进行迭代并稍后使用该可变引用?

我正在尝试迭代语句向量并匹配(或双重匹配)评估它们的结果。我认为我的错误的根本原因是迭代中的不可变借用:对于 stmt i...

回答 2 投票 0

对于类图结构最好的迭代器想法是什么?

我正在实现一个图形结构。 结构节点{ std::vector 邻居; // 返回 Graph::nodes 中节点的索引 // ... 节点信息 }; 类图{ std::向量

回答 1 投票 0

避免 TS“变量在分配之前使用”的替代语法/逻辑。

我正在尝试编写一个生成器函数,该函数将连续返回可迭代中的先前值和当前值对。 函数* prevCurrent(可迭代:可迭代){ 让他...

回答 1 投票 0

我可以在 Java 中使用适用于所有 for-each 循环的参数吗?

假设我有一个方法,它接受一个数组并使用 Java 内置的 for-each 循环处理其中的每个元素,如下所示: 公共静态无效 myFun(SomeClass[] arr) { 对于(SomeClass sc:...

回答 7 投票 0

无法取消引用值初始化的迭代器

我正在研究建议的 C++ 2d 图形库,这是我从这里获得的旧实现 https://github.com/cristianadam/io2d,我正在尝试在显示器上渲染图像表面

回答 4 投票 0


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

我的任务是解决以下问题:创建一个将实现 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

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