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示例的基础上,给合 ...
随机推荐
- php Pthread 多线程基本介绍
我们可以通过安装Pthread扩展来让PHP支持多线程. 线程,有时称为轻量级进程,是程序执行的最小单元.线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,它与同属 ...
- 【python】try...except...后中断程序继续运行
- Delphi实现树型结构
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- SpringCloud之网关 Zuul(四)
一 Zuul简介 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.Z ...
- awk中使用shell的环境变量
awk中使用shell的环境变量一:"'$var'"这种写法大家无需改变用'括起awk程序的习惯,是老外常用的写法.如:var="test"awk 'BEGIN ...
- 【Graphite】使用dropwizard.metrics向Graphite中写入指标项数据
graphite 定时向Graphite中写入指标项数据,指标项模拟个数3000个 使用的类库 官方文档 dropwizard的github地址 Metric官方文档 metrics.dropwi ...
- HTTP/HLS/RTMP超级负载测试工具
这个负载测试工具是网游分享的工具,可以在http://blog.csdn.net/win_lin/article/details/11835011 或者https://github.com/winli ...
- uoj#158. 【清华集训2015】静态仙人掌
http://uoj.ac/problem/158 预处理dfs序,询问转为区间1的个数,用可持久化bitset预处理出所有可能的修改对应哪些位置,然后用一个bitset维护当前每个点的状态,修改时可 ...
- [转].NET Framework、C#、CLR和Visual Studo之间的版本关系
原文地址:http://www.xcode.me/more/microsoft-net-framework-version-define C#版本 .NET Framework版本 CLR版本 Vis ...
- 使用jquery.mCustomScrollbar自定义滚动条(2)
参考博客:http://www.qianxingzhem.com/post-1602.html 接着上篇,另一个使用的例子,使用js来初始化滚动条,并且div中的内容可变化.需要调用相应的方法, 代码 ...