make_pair
Utilities <utility>
由短小精干的类和函数构成,执行最一般性的工作。
这些工具包括:
general types
一些重要的C函数
numeric limits Pairs
C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象
class pair可以将两个值视为一个单元。容器类别map和multimap就是使用pairs来管理其健值/实值(key/va lue)的成对元素。
pair被定义为struct,因此可直接存取pair中的个别值. 两个pairs互相比较时, 第一个元素正具有较高的优先级.
例:
namespace std{
template <class T1, class T2>
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);
}
} make_pair(): 无需写出型别, 就可以生成一个pair对象
例:
std::make_pair(, '@');
而不必费力写成:
std::pair<int, char>(, '@') 当有必要对一个接受pair参数的函数传递两个值时, make_pair()尤其显得方便,
void f(std::pair<int, const char*>); void foo{
f(std::make_pair(, '@')); //pass two values as pair
} pair的应用 pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存。另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。 pair的实现是一个结构体,主要的两个成员变量是first second 因为是使用struct不是class,所以可以直接使用pair的成员变量。 make_pair函数 template pair make_pair(T1 a, T2 b) { return pair(a, b); } 很明显,我们可以使用pair的构造函数也可以使用make_pair来生成我们需要的pair。 一般make_pair都使用在需要pair做参数的位置,可以直接调用make_pair生成pair对象很方便,代码也很清晰。 另一个使用的方面就是pair可以接受隐式的类型转换,这样可以获得更高的灵活度。灵活度也带来了一些问题如: std::pair<int, float>(, 1.1); std::make_pair(, 1.1); 是不同的,第一个就是float,而第2个会自己匹配成double。 make_pair (STL Samples)
Illustrates how to use the make_pair Standard Template Library (STL) function in Visual C++. template<class first, class second> inline
pair<first,
second> make_pair(
const first& _X,
const second& _Y
)
Remarks
NoteNote:
The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability. The make_pair STL function creates a pair structure that contains two data elements of any type. Example
复制代码
// mkpair.cpp
// compile with: /EHsc
// Illustrates how to use the make_pair function.
//
// Functions: make_pair - creates an object pair containing two data
// elements of any type. #include <utility>
#include <iostream> using namespace std; /* STL pair data type containing int and float
*/ typedef struct pair<int,float> PAIR_IF; int main(void)
{
PAIR_IF pair1=make_pair(,3.14f); cout << pair1.first << " " << pair1.second << endl;
pair1.first=;
pair1.second=1.0f;
cout << pair1.first << " " << pair1.second << endl;
}
make_pair的更多相关文章
- VS2012 error C2664: “std::make_pair”:无法将左值绑定到右值引用
		
在vs2012(c++)make_pair()改动: C++: template <class T1, class T2> pair<V1, V2> make_pair(T1& ...
 - HDU 4287 Intelligent IME(string,map,stl,make_pair)
		
题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...
 - make_pair()   (STL)
		
转载来的 Pairs C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和multimap就是使用pairs来管理其 ...
 - STL map详细用法和make_pair函数
		
今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...
 - map | make_pair
		
#include <map> void func(std::map<int,std::pair<const char*,int>> &T_map) { st ...
 - c++中vector的pair与make_pair的使用,双关键字排序
		
#include <vector> #include <iostream> #include <algorithm> using namespace std; bo ...
 - 【转】c++ make_pair函数使用
		
[好记性不如烂笔头:在<C++ Templates>看到这个函数,发现正是前段时间写项目程序所要用到的,可惜当时还不知道有这个用法,当时是自己写了个结构体..]Utilities < ...
 - C++ map.insert: pair和make_pair区别
		
C++ map.insert: pair和make_pair区别 \*********************************\ map<uint32_t, string> tem ...
 - no matching function for call to 'make_pair(std::string&, size_t&)'
		
rtl->push_back(make_pair<string, int>(str, pos)); 在redhat6上编译无问题,在centos7上编译出现错误: no matchi ...
 
随机推荐
- 24Spring_事务管理机制
			
第一部分:Spring事务管理高层抽象接口 我们介绍三个接口:1.PlatformTransactionManager 2.TransactionDefinition 3.TransactionSt ...
 - Entity Framework4.0 (一)概述(EF4 的Database First方法)
			
转自:http://www.cnblogs.com/marksun/archive/2011/12/15/2289582.html Entity Framework4.0(以后简称:EF4),是Mic ...
 - “PMS-基础权限管理系统”实施某谱OA系统经验总结
			
“PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...
 - WP老杨解迷:如何营造让人花钱的游戏
			
游戏是最好做也是最不好做的项目,游戏的好坏现在都是直接从数据来说话,Windows Phone的游戏应用同样不可能逃出这个行业准则,要说在市场里做的好,那就直接拿数据来说,几乎没人会去在乎游戏到底传达 ...
 - jacob下载问题, Office word 此文件正由另一应用程序或用户使用的解决方法
			
http://jingyan.baidu.com/article/75ab0bcbd6682fd6864db2db.html
 - 使用ConfigurationManager类读写配置文件
			
使用ConfigurationManager类 读写配置文件app.config,以下为代码: view plaincopy to clipboard print? using System; usi ...
 - Page Security
			
参见开发文档 Overview This document describes how to build applications that grant selected access to indi ...
 - UI设计师零基础入门到精通精品视频教程【155课高清完整版】
			
[福吧资源网分享]课程是非常完整的,也是非常零基础的,适合任何学员,有需要的可以下载看看!课程目录:第1章 Adobe Photoshop CS6课时1 Adobe Photoshop CS6入门基础 ...
 - [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
			
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
 - 如何在WPF中引用Windows.System.Forms.Integration
			
转自 http://www.cnblogs.com/sinozhang1988/archive/2012/11/28/2792804.html “未找到程序集 WindowsFormsIntegrat ...