草庐IT

iter_swap

全部标签

c++ - 为什么我的 swap<string,string> 比 std 版本慢得多?

这是我的C++代码:inlinestaticvoidswap(std::string&a1,std::string&a2){std::stringtemp(std::move(a1));a1=std::move(a2);a2=std::move(temp);}我运行此函数1000000次,平均耗时78毫秒,但std仅耗时13毫秒。刚刚看了下std::swap的实现,和我的差不多,为什么我的这么慢? 最佳答案 根据标准§21.3.2.8/p1swap[string.special](EmphasisMine):templatevoid

c++ - move_iterator 对于返回纯右值的迭代器被破坏并返回悬空引用

我查看了std::move_iterator的STL源代码并发现它返回Iterator::value_type&&.当Iterator::reference时,这会导致不正确的行为是右值,与Iterator::value_type&不同.我有一个带有代理对象的类reference(如std::vector),它可以隐式转换为value_type.普通迭代器只是取消对这个代理的引用(输入迭代器要求允许这样做),但是std::move_iterator调用转换为value_type带有开销,然后返回对创建的临时对象的悬空引用。std::move_iterator仍然适用于std::vect

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++ - 通过 copy-and-swap 分配与两个锁

借款HowardHinnant'sexample并将其修改为使用copy-and-swap,这op=线程安全吗?structA{A()=default;A(Aconst&x);//Assumeimplementscorrectlockingandcopying.A&operator=(Ax){std::lock_guardlock_data(_mut);usingstd::swap;swap(_data,x._data);return*this;}private:mutablestd::mutex_mut;std::vector_data;};我相信这是线程安全的(记住op=的参数是按

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++ - 为什么在此 constexpr 函数中允许使用 std::swap?

我写了一个计算两个数字的gcd的函数,它使用std::swap在第二个参数大于第一个参数的情况下。一段时间后,我意识到std::swap不是constexpr,但我的函数仍然编译并成功运行。我尝试使用MinGW-w648.1.0和VisualC++2017,它对两者都有效。我的第一个想法是因为constexpr允许在运行时执行函数,所以我尝试了std::integral_constant,它奏效了。但是,我不能使用我自己的任何非constexpr函数(这是我所期望的)。这是我的测试代码:#includeinlinevoidfoo()noexcept{}templateconstexpr

c++ - ‘operator=’ 的模糊重载与 c++11 std::move and copy and swap idiom

我收到以下错误:[matt~]g++-std=c++11main.cpp-DCOPY_AND_SWAP&&./a.outmain.cpp:Infunction‘intmain(int,constchar*const*)’:main.cpp:101:24:error:ambiguousoverloadfor‘operator=’in‘move=std::move((*©))’main.cpp:101:24:note:candidatesare:main.cpp:39:7:note:Test&Test::operator=(Test)main.cpp:52:7:note:Test&

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++ - 使用作用域 queue::swap 清空 std::queue 是否违反任何规则?

我有很多使用队列的情况,队列的大小可以增长到数百个。不幸的是,如果有必要,没有清空队列的一次性解决方案。我想知道是否使用作用域队列进行交换,然后让作用域队列被销毁,是否会破坏任何内存分配/管理规则?以下片段是我所提议的示例。似乎有效,如果长时间使用多次,则不确定结果。#include#include#includeintmain(){std::queuefoo;foo.push(10);foo.push(20);foo.push(30);std::coutbar;swap(foo,bar);}std::cout 最佳答案 您的代码没