c++ map 官方样例
#include <iostream>
#include <string>
#include <iomanip>
#include <map> template<typename Map>
void print_map(Map& m)
{
std::cout << '{';
for(auto& p : m)
std::cout << p.first << ":" << p.second << ' ';
std::cout << '}'<<std::endl;
}
struct Point
{
double x, y;
};
struct PointCmp
{
bool operator()(const Point& lhs, const Point& rhs) const
{
return lhs.x < rhs.x;
}
}; int main()
{
using namespace std;
//freopen("d://1.text", "r", stdin);
//default constructor
std::map<std::string, int> map1;
map1["something"] = ;
map1["anything"] = ;
map1["that thing"] = ;
std::cout << "map1= ";
print_map(map1); //range constructor
//从anything到结尾
map<string, int> iter(map1.find("anything"), map1.end());
cout << "\niter= ";
print_map(iter);
cout << "map1= ";
print_map(map1); //copy construct
map<string, int> copied(map1);
cout << "\ncopied= ";
print_map(copied);
cout << "map1 = ";
print_map(map1); //move construct
map<string, int> moved(std::move(map1));
cout<<endl<<"moved = "; print_map(moved);
cout<<"map1 = ";print_map(map1); //initalizer list constructor
const map<string,int> init{
{"this",},
{"can",},
{"be",},
{"const",},
};
cout<<"\ninit = ";print_map(init); //custom key class option 1;
//use a comparison struct
map<Point,double,PointCmp>mag =
{
{{,-},},
{{,},},
{{,-},}
};
for(auto p:mag)
cout<<"The magnitude of("<<p.first.x
<<", "<<p.first.y<<") is "
<<p.second<<endl; //Custom Key class option 2:
//use a comparison lambda
//This lambda sorts points according to their magnitudes, where note that
// these magnitudes are taken from the local variable mag
//声明一个比较器
auto cmplambda =
[&mag](const Point &lhs,const Point& rhs){return mag[lhs] < mag[rhs];};
//you could also use a lambda that is not dependent on local variables,like this:
//auto cmpLambda= [](const Point& lhs,const Point& rhs){return lhs.y < rhs.y;};
map<Point,double,decltype(cmplambda)>magy(cmplambda); //various ways of inserting elements:
magy.insert(pair<Point,double>({,-},));
magy.insert({{,},});
magy.insert({Point{-8.0,-15.0},});
cout<<"\n";
for(auto p :magy)
{
cout<<"The magnitude of {"<<p.first.x
<<". "<<p.first.y<<") is "
<<p.second<<"\n";
}
map<Point,double>::iterator it;
//use iterator
cout<<'\n';
for(it=magy.begin();it!=magy.end();it++)
{
cout<<"The magnitude of {"<<it->first.x
<<". "<<it->first.y<<") is "
<<it->second<<"\n";
} return ;
}
原文地址:https://en.cppreference.com/w/cpp/container/map/map
c++ map 官方样例的更多相关文章
- ShardingSphere 知识库更新 | 官方样例集助你快速上手
Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...
- HDU 1004 - Let the Balloon Rise(map 用法样例)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- 【React Native开发】React Native配置执行官方样例-刚開始学习的人的福音(8)
),React Native技术交流4群(458982758),请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...
- Boost Python官方样例(三)
导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...
- Boost Python官方样例(二)
返回值 使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_ ...
- Boost Python官方样例(一)
配置环境 $ cat /etc/os-release NAME="Ubuntu" VERSION="16.04 LTS (Xenial Xerus)" ID=u ...
- SWFUpload简单使用样例 Java版(JSP)
SWFUpload官方的样例都是PHP的,在这里提供一个Java版的最简单的使用样例,使用JSP页面完毕全部操作. 实现上传,分为三步: 1.JavaScript设置SWFUpload部分(与官方样例 ...
- Python word_cloud 样例 标签云系列(三)
转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...
- spark mllib lda 中文分词、主题聚合基本样例
github https://github.com/cclient/spark-lda-example spark mllib lda example 官方示例较为精简 在官方lda示例的基础上,给合 ...
随机推荐
- 怎样在Qsys系统中添加第三方IP核
1.新建Quartus II工程 2.新建Qsys系统 3.如果没有我们需要添加的IP,需要自己额外添加 4在我们新建的Quartus工程里面新建一个文件夹,命名为ip; 5.将我们已经下载好的IP文 ...
- 移动互联网终端的touch事件判断方向
var pressX = 0, pressY = 0; document.body.addEventListener('touchmove', function(event) { // 如果这个元素的 ...
- Django 模板语法
模板语法之变量 变量在HTML中的表示:{{var_name}} 变量取值:句点符 "." views: def index(request): import datetime s ...
- object.key 对象的键排序 可能出现的问题
// 09-集成提测工作流var node_list_info09 = { '090101': '客户端集成自测', '090201': '编译配置', '090202': '编译出包', '0903 ...
- Nexus3.6版私服搭建安装与配置教程
1.本地环境配置(Nexus3.6支持jdk版本1.6.1.7.1.8) 1.1.官网下载地址:https://www.sonatype.com/download-oss-sonatype ...
- 【Https】Spring RestTemplete支持Https安全请求
实现步骤 Step1: 自定义ClientHttpRequestFactory package com.example.demo.https; import org.springframework.h ...
- bzoj3491: PA2007 Subsets
Description 有一个集合U={1,2,…,n),要从中选择k个元素作为一个子集A.若a∈A,则要有a*X不属于A,x是一个给定的数.求可选方案对M取模后的值. 1< = N< = ...
- 学习笔记之Introduction to Data Visualization with Python | DataCamp
Introduction to Data Visualization with Python | DataCamp https://www.datacamp.com/courses/introduct ...
- 学习笔记之GenFu
Everybody was GenFu Fighting - GenFu http://genfu.io/ GenFu is a test and prototype data generation ...
- 【ZZ】号称“开发者神器”的GitHub,到底该怎么用?
号称“开发者神器”的GitHub,到底该怎么用? https://mp.weixin.qq.com/s/zpKOBMKWckY05Mv_B28RgQ A developer’s introductio ...