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示例的基础上,给合 ...
随机推荐
- (转)Mac环境下svn的使用
在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...
- Redis持久化实践及灾难恢复模拟 [转]
参考资料:Redis Persistence http://redis.io/topics/persistenceGoogle Groups https://groups.google.com/for ...
- 【ThreadLocal】使用ThreadLocal实现线程安全
非线程安全 public class UnSafeThreadLocalDemo { private int count = 0; public static void main(String[] a ...
- sklearn.cross_validation 0.18版本废弃警告及解决方法
转载:cheneyshark 机器环境: scikit-learn==0.19.1 Python 2.7.13 train_test_split基本用法 在机器学习中,我们通常将原始数据按照比例分割为 ...
- C#DateTime好用但不常用的用法
获取某年的某一个月天数 DateTime.DaysInMonth(year, i);
- PAT 乙级 1054 求平均值 (20) C++版
1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...
- NIO框架之MINA源码解析(四):粘包与断包处理及编码与解码
1.粘包与段包 粘包:指TCP协议中,发送方发送的若干包数据到接收方接收时粘成一包,从接收缓冲区看,后一包数据的头紧接着前一包数据的尾.造成的可能原因: 发送端需要等缓冲区满才发送出去,造成粘包 接收 ...
- go语言学习--指针的理解
Go 的原生数据类型可以分为基本类型和高级类型,基本类型主要包含 string, bool, int 及 float 系列,高级类型包含 struct,array/slice,map,chan, fu ...
- 小程序踩坑异步请求json时,headers设置 "content-type": "application/x-www-form-urlencoded"
wx.request({ url: url, method:params.method, data: params.data, header: { "content-type": ...
- vim查找关键字的好方法
当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 在vi里可没有菜单-〉查找 不过没关系,你在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然 ...