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 ...
随机推荐
- linux patch 格式与说明(收录)
转:http://blog.chinaunix.net/uid-26813001-id-3282954.html 首先介绍一下diff和patch.在这里不会把man在线文档上所有的选项都介绍一下,那 ...
- Linux 进程通信(无名管道)
无名管道 无名管道是半双工的,就是对于一个管道来讲,只能读,或者写. 无名管道只能在相关的,有共同祖先的进程间使用(即一般用户父子进程). 一个fork或者execve调用创建的子进程继承了父进程的文 ...
- SQL SERVER with递归示例一则
WITH SUBQUERY AS ( SELECT ORGID FROM OM_ORGANIZATION WHERE PARENTORGID = 'ROOT' UNION ALL SELECT B.O ...
- 去他的效应(what-the-hell effect)与自我放纵
去他的 效应(what-the-hell effect)与自我放纵 为什么写这篇文章: 对于我来说,但我感到疲惫——"无意拿起"手机,对自己说"随便看看"——但 ...
- Ta-lib 函数一览
import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...
- Linux常用指令---find | locate(查找)
1.locate locate指令和find找寻档案的功能类似,但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在 执行loacte时直接找该索引,查询速度会较快 ...
- PostgreSQL: 一种用于生成随机字符串的方法
create or replace function random_string(integer) returns text as $body$ select array_to_string(arra ...
- Activiti系列:如何把Activiti工程转换为maven工程以解决依赖项找不到的问题
在eclipse中安装了Activiti插件之后,就可以新建Activiti工程,但是在实际使用时发现,在该工程中间新建Activiti Diagram,绘制好该图形之后,右键,新建单元测试,选择ju ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- ASP.NET MVC Controller Session问题
发现问题 最近在项目中遇到这样一个问题,一直没办法重现,所以几天都没有解决. 测试那边给出的问题是这样的:每天早上来的时候,第一次通过单点登录到系统的时候,总会跳转回登录界面,再次登录就好了.当时给我 ...