jjzjj

c++: 释放 vector 内存,clear&swap

我读了一篇关于vector内存泄漏的博客。给定一个vector,插入大量数据后,即使删除大量数据(或者用clear()全部删除)也不会改变容器的容量,它还是会占用内存。为了避免这种情况,我们应该想办法改变容器的容量,使其与当前需要的数据保持一致。所以我写了一个测试://code1//whythisorderisbest?//memory316vectorvNum;for(inti=0;i(vNum).swap(vNum);//memory612然后我调换顺序://code2//memory308vectorvNum;for(inti=0;i(vNum).swap(vNum);//mem

c++ - 在基类和派生类中 copy-and-swap

我最近读到copy&swap现在我正在尝试在基类和派生类中实现ctors。我的基类和派生类中都有四个构造函数,但是我不确定如何实现派生类的赋值运算符。explicitBase(inti):m_i{i}{}Base(constBase&other):m_i{other.m_i}Base(Base&&other):Base(0){swap(*this,other);}Base&operator=(Baseother){swap(*this,other);return*this;}friendvoidswap(Base&a,Base&b)noexcept{usingstd::swap;swa

c++ - 尝试执行 shared_ptr swap() 时出现奇怪错误

我是一个相对的C++新手,试图将一个现有项目从原始指针转换为使用C++11的shared_ptr.总的来说进展非常顺利,我认为我理解如何shared_ptr在移动语义、右值引用等方面工作。好东西。但是,我遇到了一个奇怪的错误,我不明白也不知道如何修复。先介绍一点背景。我有一个根植于名为EidosValue的抽象基类的类层次结构,和一个名为EidosValue_Int_vector的类是(间接地)一个具体的子类:classEidosValueclassEidosValue_Int:publicEidosValueclassEidosValue_Int_vector:publicEidos

c++ - 关于将 string::swap() 与临时对象一起使用的问题

以下部分演示了我的问题:(GCC上的编译错误)stringstreamss;strings;ss我的错误:constSwap.cc:14:error:nomatchingfunctionforcallto'std::basic_string,std::allocator>::swap(std::basic_string,std::allocator>)'basic_string.tcc:496:note:candidatesare:voidstd::basic_string::swap(std::basic_string&)[with_CharT=char,_Traits=std::c

c++ - 为什么我将函数命名为 `swap` 时会出现模板错误,但 `Swap` 没问题?

好的,这是程序,绝对正确#includeusingnamespacestd;templatevoidSwap(T&a,T&b);intmain(){inti=10;intj=20;coutvoidSwap(T&a,T&b){Ttemp;temp=a;a=b;b=temp;}但是当我将函数的名称从Swap更改为swap它产生一个错误说error:callofoverloaded'swap(int&,int&)'isambiguous|note:candidatesare:voidswap(T&,T&)[withT=int]|||===Buildfinished:1errors,0warn

c++ - XOR 交换算法中运算符的未定义行为?

voidswap(int*a,int*b){if(a!=b)*a^=*b^=*a^=*b;}因为上面的*a^=*b^=*a^=*b只是*a=*a^(*b=*b^(*a=*a^*b)),可以(例如)在第三个*a之前对第二个*a进行求值(对于XOR)修改(由=)?用C99/C11/C++98/C++11写有关系吗? 最佳答案 C++11标准说:5.17/1:Theassignmentoperator(=)andthecompoundassignmentoperatorsallgroupright-to-left.(...)theassi

c++ - std::ref 和 swap 函数似乎不能很好地协同工作

templatevoidmyswap(Ta,Tb){Ttemp=a;a=b;b=temp;}intmain(){intm(20),n(30);myswap(ref(m),ref(n));//misstill20andnisstill30}为什么m和n的值没有互换?将包装在std::ref中的值传递给INCREMENT函数会导致原始变量(调用INCREMENT函数的堆栈帧中的变量)中的值发生变化。或者,std::ref的使用是否受到限制? 最佳答案 std::ref(及其关联的std::reference_wrapper)是为标准库中

c++ - vector - 当元素有 const 成员时交换两个元素

我有以下代码classa{public:constintaa;a(intaa):aa(aa){}};intmain(){std::vectorv;v.emplace_back(1);v.emplace_back(2);v.emplace_back(3);v.emplace_back(4);std::iter_swap(v.begin()+1,v.rbegin());system("pause");return0;}当我尝试交换vector的两个元素时出现错误。ErrorC2280'a&a::operator=(consta&)':attemptingtoreferenceadelete

c++ - array<> 不能简单地在内部交换指针

对于用TR1引入STL的容器数组,我有下面的问题。在“TheC++standardlibraryATutorialandReference”一书的第263页中:Note,however,thatanarraycan’tsimplyswappointersinternally.Forthisreason,swap()haslinearcomplexityandtheeffectthatiteratorsandreferencesdon’tswapcontainerswiththeirelements.我想知道为什么array不能考虑交换指针的恒定开销? 最佳答

c++ - 在 vector 之间使用 std::swap 还是 vector::swap?

给定两个std::vectorv1,v2。我想知道使用std::swap(v1,v2)比v1.swap(v2)有什么好处。我已经实现了一个关于性能观点的简单测试代码(我不确定它是否相关):#include#include#include#include#include#defineN100000templatestructTimer{templatestatictypenameTimeT::repexec(Ffunc,Args&&...args){autostart=std::chrono::steady_clock::now();func(std::forward(args)...)