#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. sqlalchemy 或者pysql 连接数据库时支持中文操作

    参数后面带上?charset=utf8就行 engine = create_engine("mysql+pymysql://hrg:123@192.168.80.200:3306/test? ...

  2. [置顶] Web用户的身份验证及WebApi权限验证流程的设计和实现 (不是Token驗證!!!不是Token驗證!!!都是基於用户身份的票据信息驗證!!!)

     转发 http://blog.csdn.net/besley/article/details/8516894 不是Token驗證!!!不是Token驗證!!!都是基於用户身份的票据信息驗證!!! [ ...

  3. Python进行RSA安装加密

    一.下载ez_setup.py(http://peak.telecommunity.com/dist/ez_setup.py) 二.用python解释执行它 (如使用IDLE打开该py文件,按F5解释 ...

  4. SoapUI 5.2.1 调试工具

    SoapUI 5.2.1 调试工具 1.打开soapUI. 2.新建一个项目,实例如下: 点击ok后在soapUI界面左侧会显示出此项目,如图: 2.创建测试用例: a.新建用例组,选择此项目右键,新 ...

  5. Django--ORM--模型增删改查--备忘

    以上运算符都区分大小写,在这些运算符前加上i表示不区分大小写,如iexact.icontains.istartswith.iendswith.insert into tb_bookinfo()valu ...

  6. vue-clil的快速搭建vue项目

    1.npm i vue-cli -g 全局安装 2.vue init webpack myProject 3.cd myProject 4.npm i 稍等片刻 完成运行下一步 5.npm run d ...

  7. [蓝桥杯]ALGO-124.算法训练_数字三角形

    问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●<三角形行数≤: ●三角 ...

  8. [蓝桥杯]ALGO-86.算法训练_矩阵乘法

    问题描述 输入两个矩阵,分别是m*s,s*n大小.输出两个矩阵相乘的结果. 输入格式 第一行,空格隔开的三个正整数m,s,n(均不超过200). 接下来m行,每行s个空格隔开的整数,表示矩阵A(i,j ...

  9. 树莓派Raspberry Pi zero w无线联网实测

    第一次学习树莓派,使用的是Raspberry Pi zero w的型号. 刚开始,就只有一块板子!!!这要怎么开发啊 经过网上查阅资料,发现可以通过WiFi连接PC端实现开发测试 准备材料:一根USB ...

  10. NodeJs递归删除非空文件夹

    此篇博文由于第一次使用fs.unlink()删除文件夹时报“Error: EPERM: operation not permitted, unlink”错误而写,这是因为fs.unlink()只能删除 ...