草庐IT

iter_swap

全部标签

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::

【JAVA】Iterator 和 ListIterator 有什么区别?

🍎个人博客:个人主页🏆个人专栏:   JAVA  ⛳️ 功不唐捐,玉汝于成目录前言 在Java中,遍历集合是日常编程中常见的任务,而Iterator和ListIterator作为遍历集合的两个主要接口,提供了不同的功能和灵活性。通过深入了解它们之间的差异,我们能够更好地选择适合特定需求的遍历方式,并充分利用它们的功能。正文在Java中,Iterator和ListIterator都是用于遍历集合元素的接口区别:适用范围:Iterator是最通用的迭代器接口,可以用于遍历任何实现了Iterable接口的集合,包括List、Set、Map等。ListIterator是Iterator的子接口,它扩展

c++ - 为什么 SGI STL 不使用 copy-and-swap 习惯用法?

我最近在StackOverflow上阅读了一个关于Whatisthecopy-and-swapidiom?的答案并且知道copy-and-swap习语可以avoidingcodeduplication,andprovidingastrongexceptionguarantee.然而,当我查看SGISTLdequeimplementation,我发现它没有使用成语。我想知道为什么不,如果这个习语在某种程度上像“最佳实践”?deque&operator=(constdeque&__x){constsize_type__len=size();if(&__x!=this){if(__len>=

c++ - 复制赋值运算符应该作为一般规则利用 std::swap 吗?

总是使用std::swap来实现我的复制赋值运算符是一个很好的通用做法吗?我的理解是,这提供了一种共享复制构造函数实现的方法。我想避免复制实际的复制逻辑本身。所以这就是我要做的:classFoo{public:Foo(Fooconst&other){/*assumevalidimplementation*/}Foo&operator=(Fooother){std::swap(*this,other);return*this;}};将“other”传递给赋值运算符的行为执行复制构造(此时我们已经共享了复制逻辑)。我假设交换将调用移动构造(这里有一个编译器生成的实现)。我几乎对每个实现复制

c++ - T::iterator 出错,其中模板参数 T 可能是 vector<int> 或 list<int>

我正在尝试编写一个函数来打印常见STL容器(vector、列表等)的表示。我给了函数一个模板参数T,例如,它可能代表vector。我在获取T类型的迭代器时遇到问题。vectorv(10,0);repr>(v);...templatevoidrepr(constT&v){cout...brett@brett-laptop:~/Desktop/stl$g++-Wallmain.cppmain.cpp:Infunction‘voidrepr(constT&)’:main.cpp:13:error:expected‘;’before‘i’main.cpp:14:error:‘i’wasnotd