Linux环境下stl库使用(map)
例子1:
testMap.cpp
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
int main()
{
map<int, string> Employees;
// 1) Assignment using array index notation
Employees[] = "Mike C.";
Employees[] = "Charlie M.";
Employees[] = "David D.";
Employees[] = "John A.";
Employees[] = "Peter Q.";
cout << ] << endl << endl;
cout << "Map size: " << Employees.size() << endl;
for( map<int,string>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second << endl;
}
}
编译与运行:
Compile: g++ testMap.cpp Run: ./a.out Employees[]=Charlie M. Map size: : David D. : Charlie M. : Mike C. : Peter Q. : John A.
例子2:
testMap2.cpp
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
int main()
{
map<string, int> Employees;
// Examples of assigning Map container contents
// 1) Assignment using array index notation
Employees[;
Employees[;
// 2) Assignment using member function insert() and STL pair
Employees.insert(std::pair<));
// 3) Assignment using member function insert() and "value_type()"
Employees.insert(map<));
// 4) Assignment using member function insert() and "make_pair()"
Employees.insert(std::make_pair());
cout << "Map size: " << Employees.size() << endl;
for( map<string, int>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second << endl;
}
}
编译与运行:
Compile: g++ testMap.cpp Run: ./a.out Map size: Charlie M.: David D.: John A.: Mike C.: Peter Q.:
testMap3.cpp
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
;
}
};
int main()
{
map<char *, int, cmp_str> Employees;
// Examples of assigning Map container contents
// 1) Assignment using array index notation
Employees[;
Employees[;
// 2) Assignment using member function insert() and STL pair
Employees.insert(std::pair<));
// 3) Assignment using member function insert() and "value_type()"
Employees.insert(map<));
// 4) Assignment using member function insert() and "make_pair()"
Employees.insert(std::make_pair(());
cout << "Map size: " << Employees.size() << endl;
for( map<char *, int, cmp_str>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second << endl;
}
}
编译与运行:
Compile: g++ testMap.cpp Run: ./a.out Map size: Charlie M.: David D.: John A.: Mike C.: Peter Q.:
testMap4.cpp
#include <iostream>
#include <map>
using namespace std;
class AAA
{
friend ostream &operator<<(ostream &, const AAA &);
public:
int x;
int y;
float z;
AAA();
AAA(const AAA &);
~AAA(){};
AAA &operator=(const AAA &rhs);
int operator==(const AAA &rhs) const;
int operator<(const AAA &rhs) const;
};
AAA::AAA() // Constructor
{
x = ;
y = ;
z = ;
}
AAA::AAA(const AAA ©in) // Copy constructor to handle pass by value.
{
x = copyin.x;
y = copyin.y;
z = copyin.z;
}
ostream &operator<<(ostream &output, const AAA &aaa)
{
output << aaa.x << ' ' << aaa.y << ' ' << aaa.z << endl;
return output;
}
AAA& AAA::operator=(const AAA &rhs)
{
this->x = rhs.x;
this->y = rhs.y;
this->z = rhs.z;
return *this;
}
int AAA::operator==(const AAA &rhs) const
{
;
;
;
;
}
int AAA::operator<(const AAA &rhs) const
{
;
;
;
;
}
main()
{
map<string, AAA> M;
AAA Ablob ;
Ablob.x=;
Ablob.y=;
Ablob.z=4.2355;
M["A"] = Ablob;
Ablob.x=;
M["B"] = Ablob;
Ablob.z=3.2355;
M["C"] = Ablob;
Ablob.x=;
Ablob.y=;
Ablob.z=7.2355;
M["D"] = Ablob;
for( map<string, AAA>::iterator ii=M.begin(); ii!=M.end(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second << endl;
}
;
}
编译与运行:
Compile: g++ testMap.cpp Run: ./a.out Output: A: 4.2355 B: 4.2355 C: 3.2355 D: 7.2355
testMap5.cpp
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
int main()
{
// Compare (<) function not required since it is built into string class.
// else declaration would comparison function in multimap definition.
// i.e. multimap<string, int, compare> m;
multimap<string, int> m;
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
cout << "Number of elements with key a: " << m.count("a") << endl;
cout << "Number of elements with key b: " << m.count("b") << endl;
cout << "Number of elements with key c: " << m.count("c") << endl;
cout << "Elements in m: " << endl;
for (multimap<string, int>::iterator it = m.begin();
it != m.end();
++it)
{
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
}
pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp;
// equal_range(b) returns pair<iterator,iterator> representing the range
// of element with key b
ppp = m.equal_range("b");
// Loop through range of maps of key "b"
cout << endl << "Range of \"b\" elements:" << endl;
for (multimap<string, int>::iterator it2 = ppp.first;
it2 != ppp.second;
++it2)
{
cout << " [" << (*it2).first << ", " << (*it2).second << "]" << endl;
}
// Can't do this (??)
// cout << ppp.first << endl;
// cout << ppp.second << endl;
m.clear();
}
编译与运行:
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
int main()
{
// Compare (<) function not required since it is built into string class.
// else declaration would comparison function in multimap definition.
// i.e. multimap<string, int, compare> m;
multimap<string, int> m;
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
m.insert(pair<));
cout << "Number of elements with key a: " << m.count("a") << endl;
cout << "Number of elements with key b: " << m.count("b") << endl;
cout << "Number of elements with key c: " << m.count("c") << endl;
cout << "Elements in m: " << endl;
for (multimap<string, int>::iterator it = m.begin();
it != m.end();
++it)
{
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
}
pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp;
// equal_range(b) returns pair<iterator,iterator> representing the range
// of element with key b
ppp = m.equal_range("b");
// Loop through range of maps of key "b"
cout << endl << "Range of \"b\" elements:" << endl;
for (multimap<string, int>::iterator it2 = ppp.first;
it2 != ppp.second;
++it2)
{
cout << " [" << (*it2).first << ", " << (*it2).second << "]" << endl;
}
// Can't do this (??)
// cout << ppp.first << endl;
// cout << ppp.second << endl;
m.clear();
}
参考:
http://www.yolinux.com/TUTORIALS/CppStlMultiMap.html
Linux环境下stl库使用(map)的更多相关文章
- Linux环境下stl库使用(vector)
step1: #include <iostream> #include <vector> #include <string> using namespace std ...
- PCL库在Linux环境下的编译安装
PCL库在Linux环境下的编译安装 PCL库的源码库:https://github.com/PointCloudLibrary/pcl 下载完了之后解压下来 编译库的几个步骤 mkdir build ...
- Linux环境下段错误的产生原因及调试方法小结(转)
最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且 项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的“段错误”(Segmentation F ...
- Linux环境下段错误的产生原因及调试方法小结
转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...
- 【转】【调试技巧】Linux环境下段错误的产生原因及调试方法小结
本文转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html 1. 段错误是什么 ...
- Linux环境下段错误的产生原因及调试方法小结【转】
转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...
- 转:Linux环境下段错误的产生原因及调试方法小结
源地址:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 补充:http://baike.baidu.com/link ...
- linux环境下学习使用pro*c/c++工具
1.proc是oracle用来预编译嵌入SQL语句的c程序. 2.如何使用proc工具 在Linux环境下,首先确保gcc编译器正常使用,安装oracle数据库或者客户端,一般就会默认安装pro*c/ ...
- 多线程编程之Linux环境下的多线程(一)
一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...
随机推荐
- (C语言)精髓——指针
(1)作用:正确而灵活的运用指针,能够有效的表示复杂的数据结构,能动态分配内存,方便地使用字符串,有效而方便地使用数组,可以直接处理内存单元地址. (2)概念:①变量的指针:变量(3)的地址.(200 ...
- SVN里常见的图标及其含义
- 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽略文件.A file ignored by version con ...
- MQTT V3.1----publish解读
客户端/服务器的发布消息行为,与PUBLISH相关的消息类型: PUBLISH 客户端发布消息经由服务器分发到所有对应的订阅者那里.一个订阅者可以订阅若干个主题(Topic name),但一个PUBL ...
- poj 1511(spfa)
---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...
- linux的提示信息--/etc/motd和/etc/issue
/etc/motd 即 message of the day 每次用户登录时,这个文件的内容都会显示在用户的终端上.如果shell支持中文,还可以使用中文,这样看起来更加舒服. 成功登录后,自动输出. ...
- 配置tomcat的虚拟路径
配置tomcat的虚拟路径有两个地方需要配置,以eclipse为例: ①在tomcat的server.xml中的host节点内添加 <Context path="/meims/user ...
- NHibernate实战详解(二)映射配置与应用
关于NHibernate的资料本身就不多,中文的就更少了,好在有一些翻译文章含金量很高,另外NHibernate与Hibernate的使用方式可谓神似,所以也有不少经验可以去参考Hibernate. ...
- 仿知乎程序 fragment的切换以及toolbar在不同页面下显示的menu不同
我们在看知乎的时候,你会发现,首页,发现,关注,收藏,草稿这五项,你在点击之后进入到相应页面之后,侧滑菜单还在,你左侧滑一下,这个侧滑菜单还在,而提问,左滑屏幕,这个页面就没有,有点像返 ...
- Java多线程题库
一. 填空题 处于运行状态的线程在某些情况下,如执行了sleep(睡眠)方法,或等待I/O设备等资源,将让出CPU并暂时停止自己的运行,进入____阻塞_____状态. 处于新建状态的线程被启动 ...
- checkbox实现全选全不选
1.jQuery实现checkbox全选全不选 <!DOCTYPE html> <head runat="server"> <title>jQu ...