pair运用
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std; typedef int KeyType; //typedef 为现有类型创建别名 ,定义易于记忆的类型名 为一种数据类型定义一个新名字
typedef std::pair<const KeyType,std::string>Pair; //定义pair对象
typedef std::multimap<KeyType,std::string>MapCode; //定义multimap对象
//multimap中的key是可以重复的,而普通map中的key不可以重复
int main()
{
MapCode codes;
codes.insert(Pair(,"San Francisco")); //插入数据
codes.insert(Pair(,"Oakland"));
codes.insert(Pair(,"Brooklyn"));
codes.insert(Pair(,"Staten Island"));
codes.insert(Pair(,"San Rafael"));
codes.insert(Pair(,"Berkeley")); cout<<"number of cities with area code 415: "<<codes.count()<<endl; //415的个数
cout<<"number of cities with area code 718: "<<codes.count()<<endl; //718的个数
cout<<"number of cities with area code 510: "<<codes.count()<<endl; //510的个数
cout<<"Area Code City\n"; MapCode::iterator it; //iterator 迭代器 提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节
for(it=codes.begin();it!=codes.end();++it) //begin()开始,end()结尾
cout<<" "<<(*it).first<<" "<<(*it).second<<endl; //(*t).first代表第一个值,(*t).second代表第二个值 pair<MapCode::iterator,MapCode::iterator> range=codes.equal_range(); //equal range 获取相等元素的子范围
cout<<"cities with area code:\n";
for(it=range.first;it!=range.second;++it) //pair::first是指向子范围左边界的迭代器 pair::last是指向子范围右边界的迭代器
cout<<(*it).second<<endl;
system("pause");
return ;
}
pair运用的更多相关文章
- c++ pair 使用
1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...
- 论Pair的重要性
这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...
- pair的使用
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...
- 【C++】pair
STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...
- hackerrank Similar Pair
传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- C++标准库 -- pair
头文件:<utility> 可访问属性: first 第一个值 second 第二个值 可访问方法: swap(pair) 和另外一个pair交换值 其他相关方法: make_pair(v ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- C++学习之Pair
C++学习之Pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型, ...
随机推荐
- android开发教程之使用线程实现视图平滑滚动示例 改
package com.melonsapp.messenger.ui.popupuser; import android.os.Handler; import android.view.View; i ...
- BUPT复试专题—最小距离查询(2013)
题目描述 给定一个由小写字母a到z组成的字符串S,其中第i个字符为S[i](下标从0开始).你需要完成下面两个操作:INSERT c 其中c是一个待输入的字符.你需要在字符串的末尾添加这个字符.保证 ...
- [Testing] JavaScript Mocking Fundamentals
Ensure Functions are Called Correctly with JavaScript Mocks Often when writing JavaScript tests and ...
- HDU2897( 巴什博奕变形)
邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- leetcode题解||Container With Most Water问题
problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- mongodb+php通过_id查询
在php中通过_id 在mongodb中查找特定记录: <?php $conn=new Mongo("127.0.0.1:27017"); #连接指定端口远程主机 $db=$ ...
- 判断IPv6地址合法性
在 <netinet/in.h> 头文件下有下列这些宏用于判断IPv6地址合法性 返回0代表true,返回非零值代表ipv6地址为非指定类型的的地址(false) int IN6_IS_A ...
- (转)我在北京工作这几年 – 一个软件工程师的反省
我于2007年来到北京,在北京工作这些年,先后在NEC.风行.百度几家公司担任软件工程师的职务.NEC是一家具有百年历史的传统日企,在知春路的分公司叫日电电子,我们部门主要从事机顶盒.数字电视上嵌入式 ...
- DRP——Dom4j使用
dom4j是一个Java的XMLAPI,类似于jdom.用来读写XML文件的.dom4j是一个很很优秀的JavaXMLAPI.具有性能优异.功能强大和极端易用使用的特点.Dom4j是一个易用的.开源的 ...
- [PHP]PDO调用存储过程
1. 数据库中已创建存储过程user_logon_check, PHP调用示例如下, <?php $dsn = 'mssql:dbname=MyDbName;host=localhost'; $ ...