1 pair 数对 make_pair()函数.. 见下面这个代码.写出来,并理解了就好了.
namespace std{
template <class _T1,class _T2>
struct pairWzy{
typedef _T1 first_type;
typedef _T2 second_type;
//member
first_type first;
second_type second;
//默认构造函数
pairWzy(): first( _T1() ), second( _T2() ){}
//两值构造函数
pairWzy(const _T1 &t1,const _T2 &t2): first(t1),second(t2){}
//用另外一个pair对象来初始化. 根据类型不同,应该用成员模板
// copy构造函数. 会发生隐式转换 如果类型完全匹配,则调用系统合成的默认copy构造函数
template<class U,class V>
pairWzy(const pair<U,V> &p): first( p.first ),second( p.second ){}
};
template <class U,class V>
bool operator== (const pairWzy<U,V> &x,const pairWzy<U,V> &y)
{
return x.first==y.first && x.second==y.second;
}
template <class U,class V>
bool operator< (const pairWzy<U,V> &x,const pairWzy<U,V> &y)
{
if( x.first< y.first)
return true;
if(x.first == y.first && x.second < y.second)
return true;
return false;
}
template <class U,class V>
pairWzy<U,V>& make_pair(const U &x,const V &y)
{
return pairWzy<U,V>(x,y);
pairWzy<U,V> p(x,y);
return p;
}
}
2. auto_ptr
1> auto_ptr指针 是"指向对象"的拥有者,拥有者被摧毁,则 对象被摧毁,一对象仅有一拥有者. 初始方式不允许赋值,必须直接初始化.
auto_ptr<int> p(new int(50)); //ok
auto_ptr<int> p=new int(50); //error
2> 拥有权转移
auto_ptr<int> ptr2(ptr1);
auto_ptr<int> ptr2=ptr1;//ptr1失去控制权,ptr2得到控制权
函数调用时,随着auto_ptr参数的传递,控制权随之发生转移. !!解决方法是用 const auto_ptr<classA> ptr; 加了const修饰符后,拥有权不能改变,当前ptr指针不能变,ptr指针指向对象的值可以变. <跟往前的const 指针 有点不同> .. 具体参考例子.
auto_ptr 完整例子:
/*
author: wzy1222
email: 627440781@qq.com
*/
#include <iostream>
#include <memory>
using namespace std;
// auto_ptr 参数用 const修饰, 拥有权才不会被转移进来
template <class T>
ostream& operator<<(ostream& os,const auto_ptr<T> &p)
{
// p.get() p是auto_ptr<T>的一个对象,用"."运算符,get得到"指向对象的指针"
// *p == * ( p.get() ) 得到指向对象的值
if( p.get() == NULL)
os<<"This is NULL";
else
os<<"The *ptr value : "<<*p;
return os;
}
int main()
{
const auto_ptr<int> p(new int(50)); //ok
//auto_ptr<int> p=new int(50); //error 赋值号相当于转移拥有权.
const auto_ptr<int> q(new int(100));
const auto_ptr<int> r;
cout<<"first time:"<<endl;
cout<<p<<endl;
cout<<q<<endl;
cout<<r<<endl;
*q=*p;
//*r=*p; //r为NULL, 没有指向对象,所以这句 undefined behavior
*p=-77;
cout<<endl<<"second time : ptr 指向的值可以改变的"<<endl;
cout<<p<<endl;
cout<<q<<endl;
cout<<r<<endl;
//q=p; //compile error const参数,ptr值不能改变,
//r=p; //compile error const参数,ptr值不能改变,
//p=NULL; //compile error const参数,ptr值不能改变,
cout<<endl<<"third time : ptr 本身不能改变 , 即拥有权不能改变"<<endl;
system("PAUSE");
return 0;
}
3. numeric_limits 数值极限的使用 <limits>
numeric_limits<T> 是一个类,里面的成员都是const 或者 static.. 可以直接类似 numeric_limits<T>::max() 这样子使用.
/*
author: wzy1222 ;
email: 627440781@qq.com
*/
#include <iostream>
#include <limits>
using namespace std;
int main()
{
numeric_limits<int> test;
cout<<test.max()<<endl;
cout<<test.min()<<endl;
cout<<numeric_limits<short>::max()<<endl;
cout<<numeric_limits<long>::max()<<endl;
system("PAUSE");
return 0;
}
4. 三个辅助函数. <algorithm>
1. 两个参数 max() min() swap()
2. 三个参数 max( int *a,int *b,cmp) ; bool cmp(int *x,int *y);
/*
author: wzy1222 ;
email: 627440781@qq.com
*/
#include <iostream>
#include <algorithm>
using namespace std;
bool cmpMax(int *t1,int *t2)
{
return *t1 < *t2;
}
int main()
{
int a=3,b=4;
cout<< max(a,b) <<endl;
cout<< min(a,b) <<endl;
// 四个指针, 三个实参都是指针,返回值也是指针~~~
cout<< *max(&a,&b,cmpMax) <<endl;
cout<<a<<b<<endl;
swap(a,b);
cout<<a<<b<<endl;
system("PAUSE");
return 0;
}