草庐IT

pair_of_ints

全部标签

c++ - 将 std::map<int, std::shared_ptr<Base>> 转换为 std::map<int, std::shared_ptr<Derived>> 的最有效安全方法

我们目前存储了几个不同的数据模型集合,如下所示:std::map>>models;字符串映射到一个已知的类型列表,这都是通过序列化处理的。嵌套映射包含“对象ID”和关联(反序列化)std::shared_ptr的集合DataObject是一个基类,我们从中派生出多种类型。我们有一个方法来获取给定类型的所有数据对象:staticstd::map>*getAll(std::stringtype);这只是在给定的“类型”键处返回指向map的指针。今天我遇到了一个代码审查来添加我认为调用UB但似乎起作用的以下内容。这让我有点紧张并寻找有效的解决方案:templatestaticstd::map

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++ - 为什么这个 EXC_BAD_ACCESS 发生在 long long 而不是 int 上?

我遇到了一个EXC_BAD_ACCESS,其中包含一段处理数据序列化的代码。该代码仅在设备(iPhone)上失败,在模拟器上不会。它还仅在某些数据类型上失败。这是重现问题的测试代码:templatevoidtest_alignment(){//allocatememoryandrecordtheoriginaladdressunsignedchar*origin;unsignedchar*tmp=(unsignedchar*)malloc(sizeof(unsignedshort)+sizeof(T));origin=tmp;//pushdatawithsizeof2bytes*((u

c++ - 模板 :Name resolution:Point of instantiation: -->can any one tell some more examples for this statement?

这是来自ISOC++标准14.6.4.1实例化点的声明Forafunctiontemplatespecialization,amemberfunctiontemplatespecialization,oraspecializationforamemberfunctionorstaticdatamemberofaclasstemplate,ifthespecializationisimplicitlyinstantiatedbecauseitisreferencedfromwithinanothertemplatespecializationandthecontextfromwhichi

c++ - 我应该使用什么类型的迭代器差异来消除 "possible loss of data"警告?

我需要一个通用的x64模式警告规则。哪种方式更好?考虑以下几行代码constintN=std::max_element(cont.begin(),cont.end())-cont.begin();或constintARR_SIZE=1024;chararr[ARR_SIZE];//...constintN=std::max_element(arr,arr+ARR_SIZE)-arr;这是我常用的代码。我对x86没有任何问题。但是如果我在x64模式下运行编译器,我会收到一些警告:conversionfrom'std::_Array_iterator::difference_type'to

c++ - 重载 ‘QString(int)’ 不明确

下面的代码片段给出了编译错误重载“QString(int)”的调用不明确与qt4.7.3(系统是linux64bit,debian不稳定)structQSAConnection{QSAConnection():sender(0),signal(0),function_ref(){}QSAConnection(QObject*send,constchar*sig,QSObjectref):sender(send),signal(QLatin1String(sig)),function_ref(ref){}QObject*sender;QStringsignal;QSObjectfunct

c++ - 使用 int 和 string boost::interprocess 映射

我有以下代码使用boost进程间将映射保存到共享内存中usingnamespaceboost::interprocess;//Sharedmemoryfront-endthatisabletoconstructobjects//associatedwithac-string.Eraseprevioussharedmemorywiththename//tobeusedandcreatethememorysegmentatthespecifiedaddressandinitializeresourcesshared_memory_object::remove("MySharedMemory

c++ - 使用 : Construction of objects at predetermined location in C++

在C++中在预定位置构造对象有什么用?以下代码说明了在预定位置的构造-void*address=(void*)0xBAADCAFE;MyClass*ptr=new(address)MyClass(/*argumentstoconstructor*/);这最终会在预定的“地址”处创建MyClass的对象。(假设address指向的存储足够大以容纳MyClass对象)。我想知道在内存中的这些预定位置创建对象的用途。 最佳答案 placementnew有用的一个场景是:您可以一次预分配大缓冲区,然后使用许多放置新运算符。这会给你更好的性能

c++ - `mpl::plus< mpl::int_<1>, mpl::int_<2>>::type` 与 `mpl::int_<3>` 不是同一类型?

下面的代码再现了一个我真的不理解boostMPL库的行为:#include#include#includeusingnamespaceboost;intmain(){typedefmpl::int_one;typedefmpl::int_two;typedefmpl::int_three;//Thefollowinglinebreakscompilation...//static_assert(is_same::type,three>::type::value,"Notthesametype");//...whilethisworksstatic_assert(mpl::plus::t

c++ - vector < vector < int >> 在第一个维度上的点积

我有vector>data_mat(3,vector(4));vectordata_vec(3);哪里data_mat可以被认为是一个矩阵和data_vec作为列vector,我正在寻找一种方法来计算data_mat的每一列的内积与data_vec,并将其存储在另一个vectordata_out(4)中.例子http://liveworkspace.org/code/2bW3X5%241使用for_each和transform,可用于计算矩阵的列和:sum=vector(data_mat[0].size());for_each(data_mat.begin(),data_mat.end