草庐IT

STL_container_type

全部标签

c++ - 转发声明/包含在模板类中 - "invalid use of incomplete type"

我正在尝试制作一个模板类,其中有一个函数接受该模板的特定实例。我做了以下人为的例子来说明这一点。比方说,我有一个标有模板化(通用)数据类型的个人世界。我有一个特定的个体,称为国王。所有个人都应该能够在国王面前下跪。一般来说,个人可以被标记为任何东西。国王用数字标记(第1、2位国王)。错误g++-g-O2-Wall-Wno-sign-compare-Iinclude-DHAVE_CONFIG_H-c-oIndividual.oIndividual.cppg++-g-O2-Wall-Wno-sign-compare-Iinclude-DHAVE_CONFIG_H-c-oKing.oKing

c++ - 如何使用时间复杂度优于 O(n^2) 的 STL vector 和 STL 算法进行左连接?

我有2个vector,其中包含Person(名字、姓氏等)对象。我想取其中一个vector(我们将其命名为“大”),然后针对该vector中的每个元素在第二个vector(“小”)中找到相应的元素,并将一些数据从“小”vector元素合并到“大”vector元素。此操作与SQL术语中的左连接非常相似,但具有额外的数据合并。最简单的方法是进行2个循环,但这会导致O(n^2)时间复杂度。我可以使用STL算法做得更好吗? 最佳答案 如果你sort小vector,然后您可以通过扫描大vector并使用binary_search获得合并部分的

c++ - GCC 用于 STL 的默认分配器是什么?

根据this链接,gcc提供了许多与STL容器一起使用的有趣的内存分配器,但如果我在创建std::list时没有指定一个,默认情况下会使用它? 最佳答案 正如您链接到的页面上所说,Thecurrentdefaultchoiceforallocatoris__gnu_cxx::new_allocator.即,默认分配器基本上只是operatornew。 关于c++-GCC用于STL的默认分配器是什么?,我们在StackOverflow上找到一个类似的问题: ht

c++ - STL vector 、迭代器和插入 (C++)

我有一个向其传递vector迭代器的方法。在这种方法中,我想将一些元素添加到vector中,但我不确定在只有迭代器时这是否可行voidGUIComponentText::AddAttributes(vector::iteratorbegin,vector::iteratorend){for(vector::iteratori=begin;i!=end;++i){GUIComponentAttribute&attrib=*(*i);//HerearetheGUIComponentAttributeobjectsanalyzed-ifanobjectofa//specialkindappe

c++ - "member of type foo has private copy constructor"错误 : why's it an error?

我试图定义这样一个类:#includeclassmy_class{private:someone_elsesfoo;public:myclass();~myclass();//...};但是编译器失败了:“someone_elses类型的字段foo有一个私有(private)的复制构造函数”。现在我知道我可以通过以下方式解决这个问题:classmy_class{private:someone_elses*foo;//...};my_class::my_class(){foo=newsomeone_elses();}my_class::~my_class(){deletefoo;}我的问

c++ - 像迭代器一样编写 STL

我试图学习像迭代器一样编写STL,为此我编写了一个简单的循环数组并在其中添加了一个迭代器。请查看代码底部以查看问题。templateclassRingQueue{T*_marray;int_mbegin;int_msize;public:RingQueue(){_marray=newT[N];_mbegin=0;_msize=0;}voidpush_back(constT&val){if(_msize!=N){_marray[(_mbegin+_msize)%N]=val;_msize++;}elsethrow"QueueFull";}Tpop_front(){if(_msize!=0

c++ - c++ STL迭代器的混淆使用

当我像这样使用迭代器时,//includeheaderfilesusingnamespacestd;intmain(){mapintIntMap;map::iteratorpos;pos=intIntMap.begin();intIntMap[0]=1;intIntMap[3]=5;intIntMap[4]=9;intIntMap[5]=5;//遍历coutfirst"second输出为4;但是当我这样使用迭代器时://includeheaderfileusingnamespacestd;intmain(){mapintIntMap;map::iteratorpos;intIntMap

c++ - 我应该使用哪个 STL 容器? C++

我有一个对象“列表”,我想从中随机获取对象并将其推到该列表的前面。只有这种操作才会被执行。所以我不需要快速访问列表的末尾,只需要它的前面和平均访问任何其他地方。哪个容器最适合这个?我在考虑std::vector,但我读到insert操作效率不高。然后我想出了std::deque因为它访问front的速度很快,但是它的eraseatspecificposition方法的效率如何呢?在此先感谢您的帮助。 最佳答案 我们可以为您提供指导,但没有明确的答案——您需要自己进行基准测试,因为这在很大程度上取决于您的收藏和对象大小:对于小对象和/

c++ - boost::container 是否支持 C++11 初始化列表?

在Boost.Container网站上我们可以看到:Boost.Containerdoesnotsupportinitializerlistswhenconstructingorassigningcontainersbutitwillsupportitforcompilerswithinitialized-listsupport.(...)对不起,如果我只是愚蠢,但我真的不知道如何阅读它。这是否意味着它会在不可预测的future这样做,或者它现在应该与符合标准的编译器一起工作?我已经在GCC4.7和ICC13.0上测试过它,两者都不起作用,但我相信我可能错过了一些非常重要的东西,比如正

c++ - 编译错误 : unresolved overloaded function type

我尝试用g++4.7.2编译以下内容:templatestructA{structB{Tt;templateTget(){returnthis->*M;}};Bb;Tget(){returnb.get();}};intmain(){Aa;a.get();}它给了我test.cpp:Inmemberfunction‘TA::get()’:test.cpp:15:23:error:expectedprimary-expressionbefore‘)’tokentest.cpp:Ininstantiationof‘TA::get()[withT=int]’:test.cpp:22:8:req