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. Python sh库学习 上篇

    官方文档有句话"allows you to call any program",并且:helps you write shell scripts in Python by givi ...

  2. HAL打开驱动失败

    --- ---

  3. 如何关闭win7的ps/2兼容鼠标(触屏版)

    买了一个新电脑联想ThinkPad E555 可是刚拿到是个win10 的系统,用习惯了win7,win0不太好用, 然后帮我刷成了win7,之后一切都好,性能也是让我很满意,但是却关不掉触控板,于是 ...

  4. Good vs Evil

    Good vs Evil Description Middle Earth is about to go to war. The forces of good will have many battl ...

  5. JAX-RS -- Java API for RESTful Web Services

    Java EE 6 引入了对 JSR-311 的支持.JSR-311(JAX-RS:Java API for RESTful Web Services)旨在定义一个统一的规范,使得 Java 程序员可 ...

  6. Nhibernate Icreteria 分页查询

    1.创建查询条件,条件为一个ICreterion的列表 ///        /// 创建Criteria(不含order,因为获取总数的时候,为了性能考虑,不加order)        ///   ...

  7. MarkdownPad2

    http://markdownpad.com/ MarkdownPad2

  8. bzoj 1879 [Sdoi2009]Bill的挑战(状压DP)

    Description  Input 本题包含多组数据. 第一行:一个整数T,表示数据的个数. 对于每组数据: 第一行:两个整数,N和K(含义如题目表述). 接下来N行:每行一个字符串. Output ...

  9. globalfifo设备驱动

    把globalmem中的全局内存变成一个FIFO,只有当FIFO中有数据的时候(即有进程把数据写到这个FIFO而且没有被读进程读空),读进程才能把数据读出,而且读取后的数据会从globalmem的全局 ...

  10. 找到的两个php爬虫,分享一下

    http://www.phpdig.net/ http://www.sphider.eu/ 虽然他们的文档我都看不太懂,甚至懒得去试一下安装,但还是留着吧,以后可能用得到吧