例子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 &copyin)   // 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)的更多相关文章

  1. Linux环境下stl库使用(vector)

    step1: #include <iostream> #include <vector> #include <string> using namespace std ...

  2. PCL库在Linux环境下的编译安装

    PCL库在Linux环境下的编译安装 PCL库的源码库:https://github.com/PointCloudLibrary/pcl 下载完了之后解压下来 编译库的几个步骤 mkdir build ...

  3. Linux环境下段错误的产生原因及调试方法小结(转)

    最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且 项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的“段错误”(Segmentation F ...

  4. Linux环境下段错误的产生原因及调试方法小结

    转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  5. 【转】【调试技巧】Linux环境下段错误的产生原因及调试方法小结

    本文转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html 1. 段错误是什么 ...

  6. Linux环境下段错误的产生原因及调试方法小结【转】

    转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  7. 转:Linux环境下段错误的产生原因及调试方法小结

    源地址:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 补充:http://baike.baidu.com/link ...

  8. linux环境下学习使用pro*c/c++工具

    1.proc是oracle用来预编译嵌入SQL语句的c程序. 2.如何使用proc工具 在Linux环境下,首先确保gcc编译器正常使用,安装oracle数据库或者客户端,一般就会默认安装pro*c/ ...

  9. 多线程编程之Linux环境下的多线程(一)

    一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...

随机推荐

  1. scp和rsync限制传输速度

    1.scp 限速100KB/s scp -l 1000 test root@192.168.1.104 此时的传输速率就是1M/8=100KB左右 2.rsync 限速100KB/s rsync -a ...

  2. 【实例】html5-canvas中实现背景图片的移动

    本例来自于<HTML5 Canvas核心技术 图形.动画与游戏开发> 在线演示 (图有点多,请多刷新几次) 本例还有一点代码不理解,我用注释和问号标注出来了,有大神看到求解答,谢谢 本例子 ...

  3. java面试宝典(蓝桥学院)

    Java面试宝典(蓝桥学院) 回答技巧 这套面试题主要目的是帮助那些还没有java软件开发实际工作经验,而正在努力寻找java软件开发工作的学生在笔试/面试时更好地赢得好的结果.由于这套试题涉及的范围 ...

  4. C# 毕业证书打印《六》

    整理思路,从新出发. 加载模版 public void loadtemplate(Label lable) { string p_tempateFile = @"fomate.xml&quo ...

  5. 《Head First Servlet JSP》学习笔记二

    一. 二. 三. 四. 五. 六. 七. 八. 九. 十. 十一. 十二.  

  6. 全局对象的构造函数会在main 函数之前执行

    #include <iostream> using namespace std; class A { public: A() { cout << "Generator ...

  7. FFmpeg frei0r water 滤镜

    FFmpeg frei0r water 滤镜, 在 linux 环境中很流畅,但在 XP 环境中抛出异常 研究一段时间修改了代码,能在 XP 里跑得动. sample.water.avi water. ...

  8. java Thread和Runnable区别

    ①Thread类实现了Runnable接口,主要构造方法为Thread(Runnable target).Thread(Runnable target,String name).Thread(Stri ...

  9. nyoj298_点的变换_错误

    点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 平面上有不超过10000个点,坐标都是已知的,现在可能对所有的点做以下几种操作: 平移一定距离(M),相对X ...

  10. 【mysql】利用Navicat for MySQL的使用

    1. 查看sql语句 如果忘记了某个SQL语句怎么写,可以利用Navicat for MySQL的历史日志来查看 在Navicat for MySQL中,直接对数据库进行想要的操作,然后点击工具-&g ...