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. 132. Palindrome Partitioning II

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  2. 弱安全协议探测工具-sslciphercheck

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为 网络通信提供安全及数据完整性的一种安全协议.TLS ...

  3. OpenCV学习笔记:如何扫描图像、利用查找表和计时

    目的 我们将探索以下问题的答案: 如何遍历图像中的每一个像素? OpenCV的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 测试用例 这里我们测试的,是一种简单的 ...

  4. Android开发之PagerAdapter

    public class ViewPagerAdapter extends PagerAdapter { private List<View> views; private Context ...

  5. cocos2dx 实用小技巧

    Menu 的 是个 很方便的 按钮 c2dx 默认为 MenuItemLabel 添加的 按下 变大 的 动画 却没有 给 MenuItemSprite 提供这样的效果 如果 自己 不想 封装新的 动 ...

  6. C#如何获取真实IP地址

    大家获取用户IP地址常用的方法是   C# 代码   复制 string IpAddress = ""; if((HttpContext.Current.Request.Serve ...

  7. POJ_3185_The_Water_Bowls_(反转)

    描述 http://poj.org/problem?id=3185 20个碗,要全部反转过来.反转某个碗,其相邻的碗(左右两边)也要反转. The Water Bowls Time Limit: 10 ...

  8. Hibernate: org.hibernate.exception.SQLGrammarException: could not insert: 错误

    最近在学习Java Web,今天刚接触Hibernate.学习的书籍是<轻量级Java EE企业应用实战(第3版)>.书中367页5.2.2中给予的Hibernate例子中的代码运行有以下 ...

  9. Oracle10g数据类型

    1.     字符类型 数据类型 长度 说明 CHAR(n BYTE/CHAR) 默认1字节,n值最大为2000 末尾填充空格以达到指定长度,超过最大长度报错.默认指定长度为字节数,字符长度可以从1字 ...

  10. 独立线程中实现QT GUI

    在网上搜集的资料: http://www.qtcentre.org/threads/16552-Starting-QT-GUI-in-a-seperate-Threadhttp://stackover ...