草庐IT

iterators

全部标签

c++ - STL 迭代器是否保证集合更改后的有效性?

假设我有某种集合,并且我在它的开头获得了一个迭代器。现在假设我修改了集合。无论集合或迭代器的类型如何,我仍然可以安全地使用迭代器吗?为避免混淆,以下是我所说的操作顺序:获取集合的迭代器。修改集合(显然不是其中的元素,而是集合本身)。使用第1步得到的迭代器,按照STL标准还有效吗?! 最佳答案 取决于容器。例如如果是vector,修改容器后所有的迭代器都会失效。但是,如果是list,则与修改的地方无关的迭代器将保持有效。Avector'siteratorsareinvalidatedwhenitsmemoryisreallocated

c++ - 插入到 STL 映射是否会使其他现有迭代器失效?

我在STL中使用了std::map。我可以在将其他元素插入map后使用迭代器吗?还有效吗? 最佳答案 如果对容器操作的语义有疑问,请咨询thedocumentation:Maphastheimportantpropertythatinsertinganewelementintoamapdoesnotinvalidateiteratorsthatpointtoexistingelements.Erasinganelementfromamapalsodoesnotinvalidateanyiterators,except,ofcours

c++ - 插入到 STL 映射是否会使其他现有迭代器失效?

我在STL中使用了std::map。我可以在将其他元素插入map后使用迭代器吗?还有效吗? 最佳答案 如果对容器操作的语义有疑问,请咨询thedocumentation:Maphastheimportantpropertythatinsertinganewelementintoamapdoesnotinvalidateiteratorsthatpointtoexistingelements.Erasinganelementfromamapalsodoesnotinvalidateanyiterators,except,ofcours

c++ - iter_swap() 与 swap() - 有什么区别?

MSDNsays:swapshouldbeusedinpreferencetoiter_swap,whichwasincludedintheC++Standardforbackwardcompatibility.但是comp.std.c++says:MostSTLalgorithmsoperateoniteratorranges.Itthereforemakessensetouseiter_swapwhenswappingelementswithinthoseranges,sincethatisitsintendedpurpose---swappingtheelementspointe

c++ - iter_swap() 与 swap() - 有什么区别?

MSDNsays:swapshouldbeusedinpreferencetoiter_swap,whichwasincludedintheC++Standardforbackwardcompatibility.但是comp.std.c++says:MostSTLalgorithmsoperateoniteratorranges.Itthereforemakessensetouseiter_swapwhenswappingelementswithinthoseranges,sincethatisitsintendedpurpose---swappingtheelementspointe

c++ - 如何使用 auto 获得 const_iterator?

第一个问题:是否可以“强制”const_iterator使用汽车?例如:std::mapusa;//...initusaautocity_it=usa.find("NewYork");我只想查询,而不是更改city_it所指向的任何内容,所以我想要city_it成为map::const_iterator.但是通过使用自动,city_it与map::find()的返回类型相同,即map::iterator.有什么建议吗? 最佳答案 抱歉,但我认为最好的建议是不完全不使用auto,因为您想要执行(隐式有效的)类型转换。auto用于推断e

c++ - 如何使用 auto 获得 const_iterator?

第一个问题:是否可以“强制”const_iterator使用汽车?例如:std::mapusa;//...initusaautocity_it=usa.find("NewYork");我只想查询,而不是更改city_it所指向的任何内容,所以我想要city_it成为map::const_iterator.但是通过使用自动,city_it与map::find()的返回类型相同,即map::iterator.有什么建议吗? 最佳答案 抱歉,但我认为最好的建议是不完全不使用auto,因为您想要执行(隐式有效的)类型转换。auto用于推断e

javascript - Angular:找不到 Promise、Map、Set 和 Iterator

安装Angular后,Typescript编译器不断收到一些关于找不到Promise、Map、Set和Iterator.直到现在我忽略了它们,但现在我需要Promise以便我的代码可以工作。import{Component}from'angular2/core';@Component({selector:'greeting-cmp',template:`{{asyncGreeting|async}}`})exportclassGreetingCmp{asyncGreeting:Promise=newPromise(resolve=>{//after1second,thepromisew

javascript - Angular:找不到 Promise、Map、Set 和 Iterator

安装Angular后,Typescript编译器不断收到一些关于找不到Promise、Map、Set和Iterator.直到现在我忽略了它们,但现在我需要Promise以便我的代码可以工作。import{Component}from'angular2/core';@Component({selector:'greeting-cmp',template:`{{asyncGreeting|async}}`})exportclassGreetingCmp{asyncGreeting:Promise=newPromise(resolve=>{//after1second,thepromisew

iterator - 在 Go 中创建迭代器最惯用的方法是什么?

一种选择是使用channel。channel在某种程度上类似于迭代器,您可以使用range关键字对其进行迭代。但是当你发现你不能在不泄漏goroutine的情况下跳出这个循环时,使用就会受到限制。在Go中创建迭代器模式的惯用方式是什么?编辑:channel的根本问题是它们是一种推送模式。迭代器是一个拉模型。您不必告诉迭代器停止。我正在寻找一种以一种很好的表达方式迭代集合的方法。我还想链接迭代器(map、过滤器、折叠替代品)。 最佳答案 channel很有用,但闭包通常更合适。packagemainimport"fmt"funcmai