<STL> pair随笔
来源:互联网 发布:msde数据库引擎 win7 编辑:程序博客网 时间:2024/05/21 00:47
<STL> pair随笔
#include <iostream>
#include <string>
using namespace std;
template<class T1,class T2>
class pair1
{
public:
T1 first;
T2 second;
//pair1():first(T1()),second(T2()){}
pair1():first(),second(){}
pair1(const T1& t1,const T2& t2):first(t1),second(t2){}
};
int main()
{
pair1<int,int>p1;
pair1<string,int>p2("hicjiajia",425);
cout<<p1.first<<" "<<p1.second<<endl;
cout<<p2.first<<" "<<p2.second<<endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
template<class T1,class T2>
class pair1
{
public:
T1 first;
T2 second;
//pair1():first(T1()),second(T2()){}
pair1():first(),second(){}
pair1(const T1& t1,const T2& t2):first(t1),second(t2){}
};
int main()
{
pair1<int,int>p1;
pair1<string,int>p2("hicjiajia",425);
cout<<p1.first<<" "<<p1.second<<endl;
cout<<p2.first<<" "<<p2.second<<endl;
system("pause");
return 0;
}
// Filename: stl_pair.h
// Comment By: 凝霜
// E-mail: mdl2009@vip.qq.com
// Blog: http://blog.csdn.net/mdl13412
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/
#ifndef __SGI_STL_INTERNAL_PAIR_H
#define __SGI_STL_INTERNAL_PAIR_H
__STL_BEGIN_NAMESPACE
// pair只是一个wraper, 所以要提供最佳效率
// 使用struct的原因是我们要能方便的存取内部元素
// pair在关联式容器中的使用极为广泛, 其本身也可以嵌套使用
template <class T1, class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair() : first(T1()), second(T2()) {}
pair(const T1& a, const T2& b) : first(a), second(b) {}
// 此版本并未提供operator =()的支持, 个人认为应该提供
#ifdef __STL_MEMBER_TEMPLATES
// 允许使用兼容的pair进行复制构造
template <class U1, class U2>
pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {}
#endif
};
// 只有当pair中的两个成员均相等时, 才判定两个pair相等
// 使用自定义类型时最好提供operator ==重载
template <class T1, class T2>
inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y)
{
return x.first == y.first && x.second == y.second;
}
// 连个pair进行比较操作时, 以第一个元素为主, 如果第一个元素不能决定表达式的值
// 那么再进行第二个元素的比较
// 使用自定义类型时最好提供operator <重载
template <class T1, class T2>
inline bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y)
{
return x.first < y.first || (!(y.first < x.first) && x.second < y.second);
}
// 至于为什么没有提供operator !=, >, >=, <=
// 这个是因为其在<stl_relops.h>中有实现, 其只依赖operator <和==
// 所以在此特化operator ==, <就能满足要求
// 提供<stl_relops.h>的作用是如果需要特化operator XXX
// 那么我们仅需要特化operator ==和<即可同时重载所有operator
// 这里使用了RVO(Return Value Optimization)机制, 如果编译器支持,
// 则可以消除临时对象的构造和析构负担
// 详细细节见<Inside The C++ Object Model>
template <class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y)
{
return pair<T1, T2>(x, y);
}
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_PAIR_H */
// Local Variables:
// mode:C++
// End:
// Comment By: 凝霜
// E-mail: mdl2009@vip.qq.com
// Blog: http://blog.csdn.net/mdl13412
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/
#ifndef __SGI_STL_INTERNAL_PAIR_H
#define __SGI_STL_INTERNAL_PAIR_H
__STL_BEGIN_NAMESPACE
// pair只是一个wraper, 所以要提供最佳效率
// 使用struct的原因是我们要能方便的存取内部元素
// pair在关联式容器中的使用极为广泛, 其本身也可以嵌套使用
template <class T1, class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair() : first(T1()), second(T2()) {}
pair(const T1& a, const T2& b) : first(a), second(b) {}
// 此版本并未提供operator =()的支持, 个人认为应该提供
#ifdef __STL_MEMBER_TEMPLATES
// 允许使用兼容的pair进行复制构造
template <class U1, class U2>
pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {}
#endif
};
// 只有当pair中的两个成员均相等时, 才判定两个pair相等
// 使用自定义类型时最好提供operator ==重载
template <class T1, class T2>
inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y)
{
return x.first == y.first && x.second == y.second;
}
// 连个pair进行比较操作时, 以第一个元素为主, 如果第一个元素不能决定表达式的值
// 那么再进行第二个元素的比较
// 使用自定义类型时最好提供operator <重载
template <class T1, class T2>
inline bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y)
{
return x.first < y.first || (!(y.first < x.first) && x.second < y.second);
}
// 至于为什么没有提供operator !=, >, >=, <=
// 这个是因为其在<stl_relops.h>中有实现, 其只依赖operator <和==
// 所以在此特化operator ==, <就能满足要求
// 提供<stl_relops.h>的作用是如果需要特化operator XXX
// 那么我们仅需要特化operator ==和<即可同时重载所有operator
// 这里使用了RVO(Return Value Optimization)机制, 如果编译器支持,
// 则可以消除临时对象的构造和析构负担
// 详细细节见<Inside The C++ Object Model>
template <class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y)
{
return pair<T1, T2>(x, y);
}
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_PAIR_H */
// Local Variables:
// mode:C++
// End:
0 0
- <STL> pair随笔
- STL Pair
- pair(STL)
- [STL] pair
- STL::pair
- STL-pair
- stl pair
- STL pair
- stl-map stl-pair
- 【STL】STL之pair
- C++ STL Pair
- C++ STL Pair
- stl pair 应用
- C++STL学习Pair
- poj2002 STL pair解法
- STL pair demo
- STL的pair
- C++ STL std::pair
- struts表单验证
- CSDN学院课程计划
- 【Android开发】图形图像处理技术-Bitmap和BitmapFactory类
- 金字塔递归算法
- JavaMail发送邮件(含附件)的例子
- <STL> pair随笔
- 学习ionic的资源和案例
- OJ题——组合题
- 偏好保存(在Activity里很简洁)
- 第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree
- Ubuntu 14.04 64bit上安装有道词典Linux版本
- 质数因子
- 用Set类判断Map里key是否存在的办法
- 日志查找自动化学习(Fighting1.1)