草庐IT

outer_iter

全部标签

c++ - m.find(...) == m.end() - 使用的是 iterator 或 const_iterator

std::mapfind/end都提供const_iterator和迭代器,例如iteratorend();const_iteratorend()const出于好奇,如果我有一个std::map,它将在这里被调用/比较,一个迭代器或一个const_iterator?:if(m.find(key)!=m.end()){...}我应该关心吗? 最佳答案 如果m是const,则返回一个const_iterator;否则将返回一个迭代器。如果您所做的只是测试map中是否存在某个元素,那么使用哪个元素并不重要。

c++ - iterator 和 const_iterator (STL) 效率不同

在Qt中有类似的类来列出map。这些类提供了一个返回const_iterator的begin_const()方法。文档说应尽可能使用这些const_iterators,因为它们速度更快。如果实例本身是const,STL只会给你一个const_iterator。只实现了一个begin()方法(为const重载)。使用iterator和const_iterator读取访问元素时有什么区别吗?(我不知道为什么它们在Qt中有区别) 最佳答案 Thedocumentationsaysthattheseconst_iteratorsshould

c++ - 在自定义迭代器上应用 reverse_iterator 后引用失效

我实现了一个双向迭代器,但它不是对数据结构进行操作,而是返回一个可以在两个方向上迭代计算的数学序列。事实上,我正在迭代整数,使用++和--在int上。这意味着数据不会存储在不同的结构中,因此当迭代器超出范围时,值也会超出范围。尽管如此,我希望下一个代码(最小失败示例)示例能够工作,因为迭代器始终保持在范围内。但它不起作用:(#include#include#includeclassmy_iterator:publicstd::iterator{intd_val=12;public:my_iteratoroperator--(int){std::cout();int&i=*it;if(t

c++ - 从 back_insert_iterator 中提取容器的 value_type 的特征类

std::back_insert_iterator的value_type等于void,但它还有一个protected成员container包含指向底层Container的指针。我正在尝试编写一个traits类来提取容器的value_type,如下所示:#include#include#includetemplatestructoutit_vt:OutputIt{usingself_type=outit_vt;usingvalue_type=typenamestd::remove_pointer_t().container)>::value_type;};intmain(){std::v

c++ - Clang 与 gcc std::crbegin with boost::iterator_range

使用libc++的Clang3.8.1编译以下程序:#include#include#include#include#includeintmain(){conststd::vectorv{1,2,3};constautorange=boost::make_iterator_range(v);std::copy(std::crbegin(range),std::crend(range),std::ostream_iterator{std::cout,""});std::cout但是带有libstdc++的gcc6.1.0没有。gcc错误的第一行是:error:nomatchingfunc

c++ - 基于范围的对 <Iterator,Iterator>

我对以下答案有疑问:https://stackoverflow.com/a/15828866/2160256如那里所述,我们不能像这样在BGL中使用基于范围的for:for(autoe:boost::edges(g))//dosomethingwithe然而,here它指出,我们可以重载使用基于范围的语义所需的begin()和end()函数。所以我尝试了:templateIbegin(std::pair&p){returnp.first;}templateIend(std::pair&p){returnp.second;}但是,编译器仍然报错:error:nomatchingfunct

c++ - 使用 istream_iterator 并从标准输入或文件中读取

我正在使用MicrosoftVisualC++编写程序,我希望我的程序使用istream_iterator从标准输入或文件中读取。谷歌搜索互联网并没有显示我认为它必须多么简单。因此,例如,我可以很容易地编写并从标准输入中读取:#include#include#includeusingnamespacestd;intmain(){istream_iteratormy_it(cin);for(;my_it!=istream_iterator();my_it++)printf("%s\n",(*my_it).c_str());}或者我可以写这个并从文件中读取:#include#include

c++ - 虚拟功能 : Iterating over a vector<Base Class> that is populated with subclass objects

简短描述:我正在迭代一个vector,在vector中的每个对象上调用一个虚函数,以执行一系列操作。vector和迭代器一样属于基类。所有的对象都是child。当调用虚函数时,它会执行基类的函数。(真的)长描述:我正在尝试为具有一组行为的生物建模。我的基类是抽象的,只有两个函数(虚拟),所有子类都已覆盖:classBehavior{public:Behavior();~Behavior(void){}virtualvoidexecute(){}virtualBEHAVIOR_TYPEgetType(){returnm_Type;}protected:BEHAVIOR_TYPEm_Typ

c++ - C++11标准中是否规定std::begin(Container&&)返回const_iterator?

这里是相关代码的链接:#include#include#include#includeintmain(){std::vectorv{1,2,3,4,5};autoiter=begin(std::move(v));if(std::is_const::type>::value)std::couthttp://coliru.stacked-crooked.com/a/253c6373befe8e50我遇到这种行为是因为declval()在decltype用std::begin表达.gcc和clang都返回迭代器,这些迭代器在取消引用时会产生const引用。这可能是有道理的,因为右值引用通常绑

c++ - 检查模板参数是否为 std::vector<T>::iterator

如何检查模板参数是否为std::vector::iterator?对于void类型,我们有std::is_void。std::vector::iterator有类似的东西吗?? 最佳答案 你可以为此创建一个特征:#include#include#includetemplatestructis_vector_iterator:std::is_same::iterator>{};templatestructis_vector_iterator(),std::enable_if_t::iterator>::value>())>:std::