草庐IT

list_container

全部标签

c++ - 可变参数模板 initilizer_list 技巧

我在visualstudio2017RC上使用C++17编写了for_each_tuple,我对这种实现感到震惊。查看:templateconstexprautofor_each_tuple(fun_t&fun,tuple_t&&tuple){std::apply([&](auto&&...args){autol={(fun(std::forward(args)),0)...};},std::forward(tuple));}intmain(){autotup=std::make_tuple(1,2,3,4);for_each_tuple([](auto&arg){++arg;},tu

c++ - 为什么 std::allocator 要求 propagate_on_container_move_assignment 为真?

根据当前标准(20.7.9),std::allocator有一个成员propagate_on_container_move_assignment设置为true_type:templateclassallocator{public:typedefsize_tsize_type;typedefptrdiff_tdifference_type;typedefT*pointer;typedefconstT*const_pointer;typedefT&reference;typedefconstT&const_reference;typedefTvalue_type;templatestruc

c++ - 为什么 vector<double> 接受带有整数元素的 initializer_list?

#include#includeintmain(){//caseI:uniforminitialization//intii=100;//Error:cannotbenarrowedfromtype'int'to'double'//ininitializerlist//doubledd{ii};//caseII:initializer_list//std::vectorvecDouble{1,2.2};//fine!//caseIII:initializer_list//std::vectorvi={1,2.3};//error:doubletointnarrowing//caseIV

c++ - 有没有一种方法可以将 container<T>::size_type 普遍用于不同类型的 T?

假设我有一个std::vectora的类(class)和std::vectorb我想要的字段reserve()在构造函数中设置为某种大小,这对于两个容器都是相等的。鉴于reserve()接受size_type参数,为了完全安全,据我所知,我必须使用两个参数编写我的构造函数,这并不是特别吸引人:MyCtor(std::vector::size_typesize1,std::vector::size_typesize2){abortIfNotEqual(size1,size2);//Proceedonlyifsize1==size2a.reserve(size1);b.reserve(si

c++ - 为什么 std::initializer_list 会复制项目?

我一直认为std::initializer_list是一个轻量级代理对象,它只会从列表项中获取const引用,而不是复制它们。但是后来我发现在这种情况下实际上执行了复制:structTest{Test(){std::cout以上代码的输出:0x63e5acdadefaultctor0x63e5acdbdefaultctor0x63e5acdcdefaultctorforbegin0x63e5acddcopyctor0x63e5acdecopyctor0x63e5acdfcopyctorCurrent:0x63e5acddCurrent:0x63e5acdeCurrent:0x63e5a

c++ - 不可复制类型的 std::initializer_list 替代品

我知道尝试使用std::initializer_list导致错误,因为元素被复制到由initializer_list表示的临时数组中.我还阅读了一些解释,说明为什么在列表中使用右值引用是不对的,我对此很满意。问题是我想传递不可复制的东西不是为了从它们中移动,而只是想const-访问它们,因此关于右值的论点不适用。如果可能的话,我该怎么做才能保留列表初始化语法和引用语义(没有包装器,没有原始指针)?NonCopyablea{...},b{...};ListInitializedc{a,b};我想我在这里遗漏了一些非常明显的东西。更新:这行得通(*),ListInitialized(std

c++ - BOOST_FOREACH : What is the error on using this of a STL container?

有谁知道为什么以下会在VC9上产生错误?classElem;classElemVec:publicvector{public:voidfoo();};voidElemVec::foo(){BOOST_FOREACH(Elem&elem,*this){//Dosomethingwithelem}return;}我得到的错误是:errorC2355:'this':canonlybereferencedinsidenon-staticmemberfunctions我现在拥有的唯一(hack)解决方案是:voidElemVec::foo(){ElemVec*This=this;BOOST_FO

c++ - QMap::contains() 未返回预期值

我有一个包含QMap对象的类:QMapusers;现在,在下面的函数Foo()中,if子句总是返回false,但是当我遍历映射时,比较的QString,即str1出现在键中。voidFoo(QString&str1,QString&str2){if(users.contains(str1))users[str1]->doStuff(str2);else{for(QMap::iteratoriter=users.begin();iter!=users.end();iter++)qDebug()我做错了什么吗?为什么contains()不返回true? 最佳答案

c# - 在 C# 委托(delegate)中编码 va_list

我正在尝试通过C#实现此功能:C头文件:typedefvoid(LogFunc)(constchar*format,va_listargs);boolInit(uint32version,LogFunc*log)C#实现:staticclassNativeMethods{[DllImport("My.dll",SetLastError=true)]internalstaticexternboolInit(uintversion,LogFunclog);[UnmanagedFunctionPointer(CallingConvention.Cdecl,SetLastError=true)

c++ - std::containers 的日志分配器?

X:我需要知道程序的每个部分使用了多少内存。我的程序经常使用C++std库。特别是,我想知道每个对象使用了多少内存。我是怎么做的:要记录some_vector的消耗,只需写my::vectorsome_vector;在哪里namespacemy{templateusingvector=std::vector>;}登录分配器实现如下:templatestructLoggingAllocator{//...boilerplate...pointerallocate(size_typen,std::allocator::const_pointerhint=0){log_allocation(