草庐IT

copy-initialization

全部标签

c++ - 使用 std::initializer_list 作为成员变量

这个问题在这里已经有了答案:Canitbesafetokeepacopyofanstd::initializer_list?Whatistherationale?(1个回答)关闭4年前。我有一个A类,它接受一个initializer_list并将其存储为一个成员变量。classA{public:A(std::initializer_listil):m_il(il){}std::initializer_listm_il;};另一个类B将A作为成员变量,默认使用initializer_list初始化classB{public:B(){std::cout现在,当我在main中运行这段代码时,

c++ - 错误 : Qualifiers dropped in binding reference of type x to initializer of type y

为什么下面会抛出这个错误:IntelliSense:qualifiersdroppedinbindingreferenceoftype"string&"toinitializeroftype"conststring".hclassA{public:wstring&GetTitle()const;private:wstringtitle;};.cppwstring&GetTitle()const{returnthis->title;}如果我删除const词,它就会停止提示,但我从未对变量进行任何更改? 最佳答案 通过返回对类成员的非c

c++ - initializer_list c++11 中的评估顺序

在下面的代码中,是否要求在f2之前调用f1(或反之亦然),还是未指定?intf1();intf2();std::initializer_listlist{f1(),f2()}; 最佳答案 这是C++标准的一个有趣的角落,其中执行顺序定义明确。第8.5.4节[dcl.init.list],第4段:Withintheinitializer-listofabraced-init-list,theinitializer-clauses,includinganythatresultfrompackexpansions(14.5.3),aree

c++ - 从多个线程为同一 vector 的不同范围调用 std::copy 是否安全?

我正在计算float来自多个线程的s并将结果存储在相同vector的非重叠范围内如下:在运行任何线程之前,我使用vector::reserve预先分配它.在每个线程中一个线程特定的vector计算结果然后将其复制到目标容器中,如下所示:vector::iteratordestination=globalVector.begin()+threadSpecificIndex;std::copy(localVector.begin(),localVector.end(),destination);这种做法安全吗? 最佳答案 首先vecto

c++ - 为什么是非法的: copying vector of pointers into a vector of pointers to constants

问题以下代码无法在C++11(或C++14)中编译。我理解编译器的错误输出,但为什么标准不允许?//main.cpp#includeintmain(void){doublea=3.0;doubleb=3.0;//Itworkswithmerepointersconstdouble*ptrToConst=&a;/***/double*ptrToObj=&a;//ptrToObj=ptrToConst;//Illegal:that'sunderstandable…ptrToConst=ptrToObj;//Works//Butthesamedoesn'tworkwithvectorstop

c++ - partial_sort_copy 是最快的 C++ 部分排序吗?

考虑以下函数,median:real_tmedian(conststd::initializer_listvars){real_ttmp[15];constunsignedx=vars.size()/2;if(x&1){std::partial_sort_copy(vars.begin(),vars.end(),&tmp[0],&tmp[x]);returntmp[x];}constunsignedy=x+1;std::partial_sort_copy(vars.begin(),vars.end(),&tmp[0],&tmp[y]);return(tmp[x]+tmp[y])/2;}

c++ - std::copy 和容器的复制构造函数之间是否存在任何性能差异?

std::copy是一种更通用的方法,因为它可以处理具有不同值类型的容器(例如,从std::vector复制到std::vector::)。但是当两个容器的值类型相同时,我是否使用复制构造函数而不是std::copy是否重要?? 最佳答案 不要担心性能,它们应该都非常接近。相反:如果您要创建一个新的拷贝容器,请使用复制构造函数或双迭代构造函数(如果元素类型不同)。如果您要替换(分配)现有容器,请使用适当的分配运算符或assign成员。如果您要替换元素的子集,请使用std::copy。通过准确地表示您正在尝试执行的操作,您可以为编译器

c++ - std::copy 的自定义插入器

给定一个包含MyClass对象的std::vector。我如何使用std::copy创建另一个仅包含MyClass的一个成员的数据的vector?我想我必须实现自定义back_inserter,但到目前为止我还不知道该怎么做。structMyClass{inta;}std::vectorvec1;//IcouldcopythattoanothervectoroftypeMyClassusingstd::copy.std::copy(vec1.begin(),vec1.end();std::back_inserter(someOtherVec)//HoweverIwantjustthed

c++ - std::copy 二维数组

您好,我正在尝试使用std::copy()函数来复制二维数组。我想知道是否可以这样做!我一直收到“段错误”,但数组已正确复制。我已经尝试减去一些并在复制函数的最终案例中添加一些,但没有成功。constintrows=3;constintcolumns=3;intmyint[rows][columns]={{1,2,3},{4,5,6},{7,8,9}};intfavint[rows][columns];std::copy(myint,myint+rows*columns,favint);很明显,“myint+rows*columns”是不正确的,事实证明这个值对应于整行,因此“myin

c++ - Visual Studio Copy Local on reference 不起作用

我在一个解决方案中有两个非托管C++DLL,分别称为A和B,并且A具有对B的引用。我想将B.dll复制到A的应用程序目录中。当我在引用上单击“复制本地”时在A的项目属性的“框架和引用”选项卡中,它看起来像是设置为true,但单击应用会将值恢复为false。知道这里有什么吗? 最佳答案 我知道已经有一段时间了,但我刚遇到这个问题并找到了这个连接页面:https://connect.microsoft.com/VisualStudio/feedback/details/766064/visual-studio-2012-copy-loc