swap函数的例子
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函数的例子的更多相关文章
- 自己写一个swap函数交换任意两个相同类型元素的值 对空指针的使用 字节大小的判断(二)了解原理
验证的代码: #include <stdio.h> int main(){ char c = 'z'; ) + (c << ) + () + 'a'; printf(" ...
- c++ swap 函数
转载地址 1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T ...
- [转]谈谈C++中的swap函数
1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...
- 【转】 谈谈C++中的swap函数
1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...
- 《Effective C++》item25:考虑写出一个不抛异常的swap函数
std::swap()是个很有用的函数,它可以用来交换两个变量的值,包括用户自定义的类型,只要类型支持copying操作,尤其是在STL中使用的很多,例如: int main(int argc, _T ...
- 条款25:考虑写出一个不抛出异常的swap函数
首先说下标准库的swap算法: namespace std{ template<typename T> void swap(T & a, T & b) { T tmp = ...
- C++ 常用编程--Swap函数有几种写法?
C++ 常用编程--Swap函数有几种写法? 在说C++模板的方法前,我们先想想C语言里面是怎么做交换的. 举个例子,要将两个int数值交换,是不是想到下面的代码: void swap(int& ...
- 关于swap函数传值的问题
#include <stdio.h> void swap(int * p3,int * p4); int main() { int a = 9; int b = 8; int * p ...
- swap函数的四种写法
swap 函数的四种写法 (1)经典型 --- 嫁衣法 void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } ( ...
随机推荐
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...
- SQL Server 连接字符串和身份验证详解
SQL Server .NET Data Provider 连接字符串包含一个由一些属性名/值对组成的集合.每一个属性/值对都由分号隔开. PropertyName1=Value1; ...
- bzoj1027
感觉网上很多题解写的似乎不清楚,这里说一下我的思路显然对于每个用户的材料(设其比例为Ai,Bi,Ci),我们要么最多用3种原料(设其比例为ai,bi,ci)混合成需要材料,要么一定混合不成,具体原因往 ...
- Win系统下制作U盘CLOVER引导+安装原版Mavericks10.9
啃苹果有一段时间了,之前一直用白苹果,但是白苹果配置有所限制,对于我搞音频的人来讲,显得有点拖沓.所以研究了将近2年的黑苹果,最近心血来潮给大家一个比较傻瓜式的教程,首先强调一点,黑苹果是需要折腾的, ...
- [HDU 1963] Investment
Investment Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Descrip ...
- jQuery与XML
jQuery与XML 快而强的遍历系统,华丽丽的选择器语法,这或许是jQuery 那么流行的原因.当然它还有详尽的文档.它主要是用来处理HTML的,但在这里妳会看到如何应用到XML. 使用jQuery ...
- lightoj 1004
很水的一个dp #include<cstdio> #include<iostream> #include<cstring> #include<algorith ...
- Entity Framework Linq 简单笔记
类型查询 public class bbb:xxx {} var items = from c in context.Dbset<xxx> where c is bbb sele ...
- 【转】Compile FFmpeg on CentOS 6.x
This guide is based on a minimal CentOS installation and will install FFmpeg with several external e ...
- 通过DeveloperApi获取spark程序执行进度及异常
在应用spark时,经常要获取任务的执行进度,可以参照jobProgressListener的设计来完成该功能. 以下代码仅供参考,欢迎交流. 效果显示: 代码: package org.apache ...