草庐IT

used_elements

全部标签

c++ - 在 C++ 模板中使用 'using' 的正确做法

我有一个内部使用多个STL列表的小类templateclassMC_base{usingOBJ_LIST=std::list;usingOBJ_VAL=typenameOBJ_LIST::value_type;usingOBJ_ITR=typenameOBJ_LIST::iterator;usingOBJ_CITR=typenameOBJ_LIST::const_iterator;OBJ_LISTA,B,C;...};使用using语句,如果我在类定义中编写一个迭代器,它看起来干净整洁:OBJ_ITRbegin(){returnA.begin();};OBJ_ITRend(){retu

c++ - 错误 C2899 : typename cannot be used outside a template declaration

我正在MSV2010中尝试以下内容namespacestatismo{templatestructRepresenterTraits,3u>>{typedefitk::Image,3u>VectorImageType;typedefVectorImageType::PointerDatasetPointerType;typedefVectorImageType::PointerDatasetConstPointerType;typedeftypenameVectorImageType::PointTypePointType;typedeftypenameVectorImageType:

c++ - 错误 "An array may not have elements of this type"

由于这个奇怪的编译错误,我在编译我的程序时遇到了问题...这是代码的具体部分://theerroroccuresat"char_adr[][]"intheconstructorparametersAddresses(string_ime,string_egn,char*_adres,char_adr[][],intadrLen):Person(_ime,_egn,_adres){addressLength=0;for(;addressLength=5){break;}adr[addressLength]=_adr[addressLength];}} 最佳答案

c++ - 警告 C4114 : same type qualifier used more than once

在将VC++6.0开发的代码迁移到VisualStudio2008时,我在代码的下面一行中收到此警告。constintconstCImportContext::PACKETSIZE=4096;我知道如何修复指针staticconstintconst*PACKETSIZE;//C4114staticconstint*constPACKETSIZE;//Correct但我的问题是如何解决这个警告,如果它像下面的警告(没有指针),staticconstintconstPACKETSIZE; 最佳答案 指针有两种不同的const限定符是有意

c++ - boost shared_ptr use_count 函数

我的申请问题如下-我有一个大结构foo。因为它们很大并且出于内存管理的原因,我们不希望在数据处理完成后删除它们。我们将它们存储在std::vector>.中我的问题与了解所有处理何时完成有关。第一个决定是我们不希望任何其他应用程序代码在结构中标记一个完整的标志,因为程序中有多个执行路径,我们无法预测哪一个是最后一个。因此在我们的实现中,一旦处理完成,我们将删除boost::shared_ptr>的所有拷贝除了vector中的那个。这会将shared_ptr中的引用计数器降为1。使用shared_ptr.use_count()查看它是否等于1以了解我的应用程序的所有其他部分何时处理完数据

c++ - (SWIG C++ 到 Python)警告 301 : class keyword used, 但不是在 C++ 模式下

我正在尝试为python编译C++扩展。我创建了一个接口(interface)文件foo.i,如下所示:%modulefoo%include"typemaps.i"//Forpointerstoprimitivetypes%include"std_string.i"//std::stringmapping%applyconststd::string&{std::string*foo};//datatypescontainingstd::stringmembers%{#defineSWIG_FILE_WITH_INIT#include"../path/to/c++/header/file

C++ STL : Why is there no upper_bound equivalent that retrieves the greatest element smaller then a specific key?

通常,STL是为提高速度而构建的。然而,在map和set数据结构上只有upper_bound和lower_bound并且没有操作来检索具有小于输入键的最大键的条目k.为什么是这样?我知道我可以简单地做一个lower_bound并做一个--it检索它,但根据数据结构,立即搜索正确的条目可能比搜索另一个条目然后返回一步更有效。例如,std::map使用红黑树,即二叉搜索树。如果upper_bound返回的元素是大于根的最小元素,则--it必须回到根,查询O(logn)的额外成本。如果这是Java,我会接受设计决定。然而,STL是为实现最高速度而构建的,那么为什么要省略此操作?澄清:我不是在

c++ - 函数指针 : is the simple canonical use bad from a performance point of view? 如果是的话,c++11-ish 的替代方案是什么?

我在我的c++代码中经常使用函数指针,总是以符合这个简单规范示例的方式使用(例如,函数具有相同的I/O,但所需的操作只是在运行时已知):#includeusingnamespacestd;intadd(intfirst,intsecond){returnfirst+second;}intsubtract(intfirst,intsecond){returnfirst-second;}intoperation(intfirst,intsecond,int(*functocall)(int,int)){return(*functocall)(first,second);}intmain()

c++ - 获取 std::min_element 和 std::max_element 以将迭代器返回到最后一个值?

当使用std::min_element和std::max_element时,如果范围内有多个元素是最低/最高,则返回的迭代器指向第一个这样的元素。但是我需要它指向最后一个这样的元素。在不编写自己的函数或反转输入数据结构的情况下,我该怎么做?我的输入数据结构是一个C风格的数组,例如intdata[N]并且C++11或Boost不可用(不是我的选择..) 最佳答案 你不必自己写数据结构,你可以使用std::reverse_iterator:typedefstd::reverse_iteratorRev;std::size_tidx=Re

c++ - 共享指针不增加 use_count

我正在尝试了解如何在C++中使用std::shared_ptr。但这很令人困惑,我不明白如何创建指向同一对象的多个共享指针。甚至文档和在线资料也不是很清楚。以下是我编写的一小段代码,用于尝试理解std::shared_ptr的行为:#include#includeusingnamespacestd;classNode{public:intkey;Node(){key=0;}Node(intk){key=k;}};intmain(){Nodenode=Node(10);shared_ptrptr1((shared_ptr)&node);coutptr2=make_shared(node)