草庐IT

possible_copy

全部标签

c++ - std::copy 用于动态分配的指针

这可能是一个非常新手的问题。我正在处理的类(我们称之为MyClass)包含以下变量intnbins;double*bins;//=newdouble[nbins+i]因为我正在实现一个复制构造函数MyClass(constMyClass&source)我需要复制source.bins。我打算使用algorithm中的std::copy,但我发现的所有示例都展示了如何复制静态数组,如doublebins[]=。..或标准数据结构,如std::vector。我找不到任何示例,其中std::copy用于像double*bins=newdouble[nbins+i]这样的指针。所以,我的问题是

C++ move 语义 : why copy assignment operator=(&) is called instead of move assignment operator=(&&)?

我有以下代码:#include#includeusingstd::cout;structSomeType{SomeType(){}SomeType(constSomeType&&other){cout我希望move构造函数调用move赋值运算符。下面是这个程序的输出:SomeType(SomeType&&)operator=(constSomeType&)operator=(SomeType&&)如您所见,move赋值运算符已成功调用,但在move构造函数内分配给*this时未成功调用。为什么会发生这种情况,我能以某种方式解决它吗? 最佳答案

c++ - 返回值优化 : ho can I avoid copy construction of huge STL containers.

当我想要一个函数返回一个容器时:vectorfunc(){vectorresult;...returnresult;}按以下方式使用:vectorresult=func();为了避免复制我的容器的开销我经常编写函数,以便它只返回接受一个容器的非常量实例。voidfunc(vector&result){result.clear();...result;}按以下方式使用:vectorresult;func(result);难道我的努力没有意义,因为我可以确定编译器总是使用返回值优化? 最佳答案 没有意义。你提到的RVO类型称为命名RVO

c++ - std::copy 和 std::vector 问题

我明白为什么这会导致段错误:#include#includeusingnamespacestd;intmain(){vectorv;intiArr[5]={1,2,3,4,5};int*p=iArr;copy(p,p+5,v.begin());return0;}但为什么这不会导致段错误?#include#includeusingnamespacestd;intmain(){vectorv;intiArr[5]={1,2,3,4,5};int*p=iArr;v.reserve(1);copy(p,p+5,v.begin());return0;} 最佳答案

c++ - 使用 std::ifstream、std::istream_iterator 和 std::copy 不读取整个文件

我在一个188字节的文件中使用了以下代码:std::ifstreamis("filename",std::ios::binary);std::vectorbuffer;std::istream_iteratori_input(is);std::copy(i_input,std::istream_iterator(),std::back_inserter(buffer));std::cout但是它只读取了188个字节中的186个字节。我已经在十六进制编辑器和ls-al中确认了文件大小。 最佳答案 我不知道为什么,但默认情况下似乎会跳过

c++ - 为什么 C++ 标准 1.9/5 谈论 "possible execution sequences"?

根据C++03标准1.9/5Aconformingimplementationexecutingawell-formedprogramshallproducethesameobservablebehaviorasoneofthepossibleexecutionsequencesofthecorrespondinginstanceoftheabstractmachinewiththesameprogramandthesameinput.我不明白“作为其中一个”部分。如果我有一个特定的程序和一个特定的输入,并且我的程序不包含未定义的行为,为什么可观察到的行为会有所不同?“一种可能的执行顺

c++ - QFile::copy() 的进度条?

我正在制作一个在Qt中复制文件的程序。我想知道如何将QProgressBar与boolQFile::copy(constQString&fileName,constQString&newName)一起使用。这甚至可以通过copy函数实现吗?可以暂停复制过程吗? 最佳答案 你不能使用静态QFile::copy()方法来做到这一点。正如Maciej所说,您需要编写自己的类(class)。它应该使用两个QFile对象,一个用于读取,一个用于写入。分部分传输数据(例如整个文件大小的1%)并在每个部分后发出进度信号。您可以将此信号连接到进度对

c++ - 为什么 std::is_copy_constructible 的行为不如预期?

#includeintmain(){std::is_constructible_v;//false,asexpected.std::is_copy_constructible_v;//true,NOTasexpected!}根据cppref:IfTisanobjectorreferencetypeandthevariabledefinitionTobj(std::declval()...);iswell-formed,providesthememberconstantvalueequaltotrue.Inallothercases,valueisfalse.std::is_copy_c

c++ - STL 容器 move 语义并按值返回 : how many times of copying get avoided away?

我知道在C++11中,move语义已经在STL容器中实现以避免临时对象。人们说现在编写按值返回的函数是完美的。但我对究竟有多少次复制实际上被避免感到困惑。请看下面的例子:vectormyVector(){vectorres;res.push_back(4);res.push_back(5);returnres;}vectorv=myVector();我的理解是在c++03中,myVector返回res的拷贝(4,5复制了一次),在评估vectorv=myVector();时vector的复制构造函数vector(constvector&)被调用(4,5复制了两次)。但是在具有move语

c++ - 现代 C 和 C++ : it is possible to use one defined structure for other declared structure?

假设我想制作某种支持加载图形Image的引擎,所以我有structImage;Image*load_image_from_file(...);我不想让外部世界知道Image到底是什么,他们只会处理指向它的指针。但是在engine内部我想使用特定的类型,例如SDL_Surface在SDL中完全定义。我能否以某种方式重新定义此文件的图像,以便编译器在每次看到Image*(宏除外)时都假定为SDL_Surface*?即我想要像typedefstructSDL_SurfaceImage;这样的东西所有的尝试都像usingImage=SDL_Surface;typedefSDL_SurfaceI