13.31为你的HasPtr类定义一个<运算符,并定义一个HasPtr的vector为这个vector添加一些元素,并对它执行sort。注意何时会调用swap。

#include<iostream>
#include<string>
#include<new>
#include<vector>
#include<algorithm> using namespace std;
class HasPtr
{
friend void swap(HasPtr&,HasPtr&);
public:
HasPtr(const string &s=string()):ps(new string(s)),i(){cout<<"constructer"<<endl;}
HasPtr(const HasPtr &h):i(h.i)
{
cout<<"copy constructer"<<endl;
ps=new string;
*ps=*h.ps;//只拷贝值
}
HasPtr& operator=(HasPtr h)
{
swap(*this,h);
return *this;
}
bool operator<(const HasPtr &h) const
{
return i<h.i;
}
~HasPtr() { delete ps; cout<<"destructer"<<endl;}
private:
string *ps;
int i;
};
void swap(HasPtr &lhs,HasPtr &rhs)
{
cout<<"swap"<<endl;
using std::swap;
swap(lhs.ps,rhs.ps);
swap(lhs.i,rhs.i);
}
int main()
{
HasPtr h;
HasPtr hh(h);
hh=h;
swap(h,hh);
vector<HasPtr> vec={h,hh};
sort(vec.begin(),vec.end());
return ;
}

swap函数的例子的更多相关文章

  1. 自己写一个swap函数交换任意两个相同类型元素的值 对空指针的使用 字节大小的判断(二)了解原理

    验证的代码: #include <stdio.h> int main(){ char c = 'z'; ) + (c << ) + () + 'a'; printf(" ...

  2. c++ swap 函数

    转载地址 1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T ...

  3. [转]谈谈C++中的swap函数

    1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...

  4. 【转】 谈谈C++中的swap函数

    1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...

  5. 《Effective C++》item25:考虑写出一个不抛异常的swap函数

    std::swap()是个很有用的函数,它可以用来交换两个变量的值,包括用户自定义的类型,只要类型支持copying操作,尤其是在STL中使用的很多,例如: int main(int argc, _T ...

  6. 条款25:考虑写出一个不抛出异常的swap函数

    首先说下标准库的swap算法: namespace std{ template<typename T> void swap(T & a, T & b) { T tmp = ...

  7. C++ 常用编程--Swap函数有几种写法?

    C++ 常用编程--Swap函数有几种写法? 在说C++模板的方法前,我们先想想C语言里面是怎么做交换的. 举个例子,要将两个int数值交换,是不是想到下面的代码: void swap(int& ...

  8. 关于swap函数传值的问题

    #include <stdio.h> void swap(int * p3,int * p4); int main() {  int a = 9;  int b = 8;  int * p ...

  9. swap函数的四种写法

    swap 函数的四种写法 (1)经典型 --- 嫁衣法 void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } ( ...

随机推荐

  1. 如何忽略usb host 模式设备连接确认对话框

    <li class="alt"><span><span>package android.hardware.usb;  </span> ...

  2. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  3. mysql联合索引

    命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果where条件中是OR关系,加索引不起作用4.符合最左原则 https://segmentfaul ...

  4. Maprduce重写参考

    Maprduce数据流走向图:   流程解释    Input files        功能描述:存储在HDFS中的文件数据        InputFormat            功能描述:1 ...

  5. bzoj1922

    首先机器人是并行的: 很容易想到到某个点的最短用时 =max(到这个点的最短路,max(到保护这个点结界所在点的最短用时)) 所以我们在做dij的时候,d[j]维护最短路,w[j]维护所有保护这个点结 ...

  6. 执行sql update use c#

    今天犯了个大错 public static void ChangeGoodsCounts(int GoodsID, int changCounts) { int lastCount; using (S ...

  7. 为EF DbContext生成的实体添加注释(T5模板应用)[转]

    1 先加上类注释 找到这行代码WriteHeader(codeStringGenerator, fileManager): 在它下面加上我们的代码: string summary=string.Emp ...

  8. Android 不能勾选 Project Build Target

    再勾选完project bulid target,从新返回这个页面,发现还是没有被勾选上. 从新刷新一下项目,原因是project.properties配置文件没有加载上. 下回导入的项目第一件事就是 ...

  9. Mac OS X Mountain Lion安装Bochs

    基本步骤可以看这个帖子 http://hi.baidu.com/any_where/item/990c0acdfbd6542c47d5c003 大体是: 1.安装x11; 2.开启Mac OS X的r ...

  10. Html笔记(九)头标签

    头标签: <head> 头标签都放在 <head> </head> 头部分之间.包括:title base meta link <title> :指定浏 ...