草庐IT

stl-algorithm

全部标签

c++ - 带有自定义分配器的 const T 的 STL 容器是否格式错误?

采用以下代码片段:#includestd::vectorgood;//illegal,becausestd::allocatorisill-formedstd::vectorbad;//failstocompileunderMSVS2017std::vectorX;为什么X编译失败?MSVS2017展会ErrorC2338(failedstaticassert):TheC++Standardforbidscontainersofconstelementsbecauseallocatorisill-formed.据我了解,这不一定是正确的。根据20.5.3.5[allocator.req

符合 C++ STL 的分配器

在处理小对象时,有哪些分配器可用于STL。我已经尝试过使用Boost的池分配器,但没有得到任何性能提升(实际上,在某些情况下性能下降相当大)。 最佳答案 你没有说你使用的是什么编译器,但它可能带有一堆预构建的分配器。这是在带有gcc4.2.1的Mac上:~$find/usr/include/c++/4.2.1/-name"*allocator*"/usr/include/c++/4.2.1/bits/allocator.h/usr/include/c++/4.2.1/ext/array_allocator.h/usr/include

c++ - STL - 以下代码的问题是什么?

#include"stdafx.h"#include#includeusingnamespacestd;classNiftyEmailProgram{private:typedefmapNicknameMap;NicknameMapnicknames;public:voidShowEmailAddress(conststring&nickname)const{NicknameMap::const_iteratori=nicknames.find(nickname);if(i!=nicknames.end()){}}};intmain(intargc,char*argv[]){print

c++ Bresenham's line algorithm 绘制圆弧和旋转

我正在寻找使用Bresenham直线算法制作圆弧的方法。该算法绘制完美的圆,但是如果我需要绘制圆弧(从0到Pi)并将其旋转30度(例如)怎么办?voidDrawCircle(HDChdc,intx0,inty0,intradius){intx=0;inty=radius;intdelta=2-2*radius;interror=0;while(y>=0){//SetPixel(hdc,x0+x,y0+y,pencol);SetPixel(hdc,x0+x,y0-y,pencol);//SetPixel(hdc,x0-x,y0+y,pencol);SetPixel(hdc,x0-x,y0

用于数组、 vector 、内置类型、STL 的 C++ 模板函数

我想创建一个类,该类具有将发送给它的数据保存到文本文件的功能。可以传递给它的数据可以是std::string之类的任何东西。,int,double,float,unsignedint等,也可以是:std::vector,T[],和std::vector>,T[][].现在,很明显,如果它是一个数组,我将需要遍历它以将数据发送到文本文件。我在考虑使用模板,但我不确定如何使用数组。这是要走的路吗?classCMyClass{templatevoidSaveData(Tdata);voidSaveData(std::vectordata);voidSaveData(std::stringda

c++ - 在 C++ 中使用 next_permutation() STL 在两个数组中进行相同的排列

有没有一种简单的方法可以使next_permutation执行相同的一组交换两个相同大小的不同数组例如如果我有两个数组a[]={1,2,3,4,5}和b[]={12,23,21,2,3}如果排列后数组a中的1到达第3个位置,那么数组b中的12也应该到达第3个位置。 最佳答案 可以做一个辅助索引集:inta[]={1,2,3,4,5};intb[]={12,23,21,2,3};std::size_tindices[]={0,1,2,3,4};现在对indices进行排列,然后使用a[indices[i]]和b[indices[i]]

C++ STL 集 : Compare object with extrinsic state

这个定义在OuterClass中:structCompare{booloperator()(constT&,constT&);};typedefsetMySet;我的问题是比较函数operator()取决于OuterClass的状态。(MySet实例在优化算法期间使用,它们必须在不同阶段以不同方式排序。)是否有任何方法/变通方法可以从比较函数operator()中访问OuterClass的非静态成员? 最佳答案 Isthereanyway/workaroundtoaccessnonstaticmembersofOuterClassf

c++ - STL 实现中是否有中心分配双端队列或 vector ?

我正在阅读deques与vectors,并遇到了它的wikipediaentry,表示deque的三种可能实现之一。使用动态数组是:Allocatingdequecontentsfromthecenteroftheunderlyingarray,andresizingtheunderlyingarraywheneitherendisreached.Thisapproachmayrequiremorefrequentresizingsandwastemorespace,particularlywhenelementsareonlyinsertedatoneend.我想知道是否有任何实际使

c++ - MSVC 2017 中 STL 容器的移动构造函数未标记为 noexcept

我正在将我的项目从VS2015迁移到VS2017,这当然不会很顺利。我看到奇怪的编译器错误,可以通过以下代码重现:structMoveOnly{MoveOnly(){}MoveOnly(constMoveOnly&)=delete;MoveOnly&operator=(constMoveOnly&)=delete;MoveOnly(MoveOnly&&)=default;MoveOnly&operator=(MoveOnly&&)=default;booloperator==(constMoveOnly&rhs)const{returnfalse;}};structHasher{siz

c++ - STL std::remove_if 编译器失败

我无法让std::remove_if进行编译,如您所见,我选择了另一种工作正常的手摇曲柄方法,编译器错误位于列表底部的代码之后。任何帮助将不胜感激。谢谢,汤姆#include#include#include#include#include////Findthelargestcompoundwordcomposed//ofsub-wordsfromalist.////-readlistfromfile.////PsuedoCode:////1.ReadNextWordfromFile.//2.Searchinlistforwordformedfromword.//3.ifFoundinL