#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 官方样例的更多相关文章

  1. ShardingSphere 知识库更新 | 官方样例集助你快速上手

    Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...

  2. HDU 1004 - Let the Balloon Rise(map 用法样例)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  3. 【React Native开发】React Native配置执行官方样例-刚開始学习的人的福音(8)

    ),React Native技术交流4群(458982758),请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...

  4. Boost Python官方样例(三)

    导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...

  5. Boost Python官方样例(二)

    返回值 使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_ ...

  6. Boost Python官方样例(一)

    配置环境 $ cat /etc/os-release NAME="Ubuntu" VERSION="16.04 LTS (Xenial Xerus)" ID=u ...

  7. SWFUpload简单使用样例 Java版(JSP)

    SWFUpload官方的样例都是PHP的,在这里提供一个Java版的最简单的使用样例,使用JSP页面完毕全部操作. 实现上传,分为三步: 1.JavaScript设置SWFUpload部分(与官方样例 ...

  8. Python word_cloud 样例 标签云系列(三)

    转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...

  9. spark mllib lda 中文分词、主题聚合基本样例

    github https://github.com/cclient/spark-lda-example spark mllib lda example 官方示例较为精简 在官方lda示例的基础上,给合 ...

随机推荐

  1. 源码查看工具ctags+vim

    一.ctags源码工具的安装 1.sudo apt-get install ctags 2.源码目录如下: 3.对配置文件进行配置 4.在末尾添加如下: 二.使用 1. 进入源码目录,输入ctags ...

  2. KC705开发板关于MIG的配置

    KC705开发板关于MIG的配置

  3. 解决nginx转发websocket报400错误

    解决nginx转发websocket报400错误 说明 由于个人服务器上面有多个项目,配置了二级域名,需要对二级域名进行转发,在转发工作这快采取了大名鼎鼎的nginx.在这之前所有的项目运行转发都没问 ...

  4. vue cli 配置信息说明

    摘自csdn http://blog.csdn.net/hongchh/article/details/55113751

  5. Hadoop 管理工具HUE配置-filebrowser配置

    Hue提供了图形化截面管理HDFS数据,可谓之非常方便,但是在配置上,还是有点麻烦的. 1 /home/hadoop/software/cloud/hue/desktop/conf/pseudo-di ...

  6. Ubuntu 14.10 下安装伪分布式hbase 0.99.0

    HBase 安装分为:单击模式,伪分布式,完全分布式,在单机模式中,HBase使用本地文件系统而不是HDFS ,所有的服务和zooKeeper都运作在一个JVM中.本文是安装的伪分布式. 安装步骤如下 ...

  7. DB2 Rename SQL0108N错误

    今天DB2 Rename的时候遇到了“SQL0108N ‘表名’名称的限定符数是错误的. 语句如下: db2 rename tableA to tableB 异常: SQL0108N The name ...

  8. mysql查询优化之四:优化特定类型的查询

    本文将介绍如何优化特定类型的查询. 1.优化count()查询count()聚合函数,以及如何优化使用了该函数的查询,很可能是mysql中最容易被误解的前10个话题之一 count() 是一个特殊的函 ...

  9. 廖雪峰Java5集合-4Set-1使用Set

    集合 Set用于存储不重复的元素集合: boolean add(E e) boolean remove(Object o) boolean contains(Object o) int size() ...

  10. mysql 意向锁的作用

    直接copy知乎上的内容 https://www.zhihu.com/question/51513268 作者:尹发条地精链接:https://www.zhihu.com/question/51513 ...