草庐IT

iter_swap

全部标签

c++ - 什么是复制省略,它如何优化 copy-and-swap 的习惯用法?

我正在阅读CopyandSwap.我尝试阅读有关CopyElision的一些链接,但无法正确理解其含义。有人可以解释一下这个优化是什么,尤其是下面的文字是什么意思Thisisnotjustamatterofconveniencebutinfactanoptimization.Iftheparameter(s)bindstoalvalue(anothernon-constobject),acopyoftheobjectismadeautomaticallywhilecreatingtheparameter(s).However,whensbindstoarvalue(temporaryo

c++ - Boost 1.46.1,属性树 : How to iterate through ptree receiving sub ptrees?

首先我要说我认为我知道应该怎么做,但是我的代码不会以我尝试的任何方式编译。我的假设基于thisofficialexampleofemptyptreetrick.在那里你可以找到下一行:constptree&settings=pt.get_child("settings",empty_ptree());这表明可以(或应该)从ptree中取出subptree。所以我假设我们可以用类似BOOST_FOREACH这样的方式遍历ptree:BOOST_FOREACH(constboost::property_tree::ptree&v,config.get_child("servecies"))

c++ - iter_swap 的意义何在?

我只是想知道,为什么有人会写这个:std::iter_swap(i,k);而不是这个?std::swap(*i,*k);//savedafewkeystrokes!然后我研究了iter_swap的实现,当然它只使用swap而不是std::swap因为我们是无论如何,已经在namespacestd中了。这就引出了下一个问题:为什么会有人写这个:usingstd::swap;swap(a,b);而不是这个?std::iter_swap(&a,&b);//savedanentirelineofcode!我在这里忽略了任何重要的差异/问题吗? 最佳答案

c++ - CppCon 2018,尼古拉 Josuttis : Why are these interpreted as iterators?

NicolaiJosuttis在CppCon2018上的“C++初始化的噩梦”演讲曾一度包含以下代码:std::vectorv07={{"1","2"}};尼古拉saidthefollowing(我的转录本):Theproblemis,whathappenshereis,weinterpretthesetwoparametersasiterators.Sotheseareiterators,sothisisthebeginningoftherange,andthisistheendoftherange,andtheyshouldrefertothesamerangeofcharacte

c++ - 既然我们已经有了 move 语义,那么专门化 std::swap 了吗?

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:Movesemantics==customswapfunctionobsolete?这就是std::swap在C++11中的样子:templatevoidswap(T&x,T&y){Tz=std::move(x);x=std::move(y);y=std::move(z);}我是否仍然需要为我自己的类型专门化std::swap,或者std::swap是否尽可能高效,前提是我的类定义当然是move构造函数和move赋值运算符? 最佳答案 std::swap的特

c++ - const_iterators 更快吗?

我们的编码指南更喜欢const_iterator,因为它们比普通的iterator快一点。当您使用const_iterator时,编译器似乎会优化代码。这真的正确吗?如果是,那么内部究竟发生了什么使const_iterator更快?。编辑:我写了一个小测试来检查const_iterator与iterator并发现不同的结果:迭代10,000个对象const_terator减少了几毫秒(大约16毫秒)。但并非总是。有一些迭代是相等的。 最佳答案 如果没有别的,const_iteratorreads更好,因为它告诉任何阅读代码的人“我只

c++ - 为什么不推荐使用 std::iterator ?

模板类std::iterator在C++17中设置为弃用。为什么这样?这是确保std::iterator_traits的便捷方法。有效,特别是如果您可以使用默认模板参数。在C++17中还有其他方法吗? 最佳答案 来自theproposalthatsuggesteditsdeprecation:Asanaidtowritingiteratorclasses,theoriginalstandardlibrarysuppliedtheiteratorclasstemplatetoautomatethedeclarationofthefiv

C++ 流混淆 : istreambuf_iterator vs istream_iterator?

istreambuf_iterator和istream_iterator有什么区别?一般来说,流和流缓冲区有什么区别?我真的找不到任何明确的解释,所以决定在这里问。 最佳答案 IOstreams使用streambufs作为输入/输出的源/目标。实际上,streambuf-family完成了有关IO的所有工作,而IOstream-family仅用于格式化和to-string/from-string转换。现在,istream_iterator接受一个模板参数,该参数说明来自streambuf的未格式化字符串序列应该被格式化为什么格式,例

c++ - std::swap vs std::exchange vs 交换运算符

std::swap的实现可能如下所示:templatevoidswap(T&a,T&b){Tc(std::move(a));a=std::move(b);b=std::move(c);}templatevoidswap(T(&a)[N],T(&b)[N]){for(size_ti=0;i一个实现std::exchangen3668可能看起来像这样:templateTexchange(T&obj,U&&new_val){Told_val=std::move(obj);obj=std::forward(new_val);returnold_val;}上面写着:Forprimitivetyp

c++ - 如何删除 const_iterator 的常量?

作为这个问题的延伸Areconst_iteratorsfaster?,我还有一个关于const_iterators的问题.如何删除const_iterator的常量?虽然迭代器是指针的广义形式,但仍然是const_iterator和iterators是两个不同的东西。因此,我相信,我也不能使用const_cast隐藏const_iterator至iterator秒。一种方法是定义一个迭代器,它移动'直到const_iterator的元素。点。但这看起来是一个线性时间算法。您知道实现这一目标的最佳方法是什么吗? 最佳答案 在C++11