STL map mulimap

Sparse array example: (why hold space for thousands of elements when all we have is five)

01#include <string.h>
02#include <iostream>
03#include <map>
04#include <utility>
05 
06using namespace std;
07 
08int main()
09{
10   map<int, string> Employees;
11 
12   // 1) Assignment using array index notation
13   Employees[5234] = "Mike C.";
14   Employees[3374] = "Charlie M.";
15   Employees[1923] = "David D.";
16   Employees[7582] = "John A.";
17   Employees[5328] = "Peter Q.";
18 
19   cout << "Employees[3374]=" << Employees[3374] << endl << endl;
20 
21   cout << "Map size: " << Employees.size() << endl;
22 
23   for( map<int,string>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)
24   {
25       cout << (*ii).first << ": " << (*ii).second << endl;
26   }
27}

Compile: g++ testMap.cpp
Run: ./a.out

Employees[3374]=Charlie M.  Map size: 5 1923: David D. 3374: Charlie M. 5234: Mike C. 5328: Peter Q. 7582: John A. 
Example using a "string" as an array index:
01#include <string.h>
02#include <iostream>
03#include <map>
04#include <utility>
05 
06using namespace std;
07 
08int main()
09{
10   map<string, int> Employees;
11 
12   // Examples of assigning Map container contents
13 
14   // 1) Assignment using array index notation
15   Employees["Mike C."] = 5234;
16   Employees["Charlie M."] = 3374;
17 
18   // 2) Assignment using member function insert() and STL pair
19   Employees.insert(std::pair<string,int>("David D.",1923));
20  
21   // 3) Assignment using member function insert() and "value_type()"
22   Employees.insert(map<string,int>::value_type("John A.",7582));
23 
24   // 4) Assignment using member function insert() and "make_pair()"
25   Employees.insert(std::make_pair("Peter Q.",5328));
26 
27   cout << "Map size: " << Employees.size() << endl;
28 
29   for( map<string, int>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)
30   {
31       cout << (*ii).first << ": " << (*ii).second << endl;
32   }
33}

Compile: g++ testMap.cpp
Run: ./a.out

Map size: 5 Charlie M.: 3374 David D.: 1923 John A.: 7582 Mike C.: 5234 Peter Q.: 5328 

Note: The fully defined STL map defines a comparison operator in the map declaration so that the indexes can be ordered. This is used to provide a default comparison operator for the data type. In the above example, the key type is an integer and the C++ "equals" (=) and "less than" operator (<) can operate on an integer so the example works. The use of the STL algorithm std::less<> can be specified explicitly as in the following declaration:

std::map<int, string, std::less< int > >
If defining your own class as the index (first value), C++ will not know how to perform the comparison, thus you will have to provide an operator to perform this function.

The first element in a map can be a class or even another STL container such as a pair. If using a class, the class must provide an overloaded "equals" (=) and "less than" operator (<).

Thus using "char" instead of "string" requires the use of a comparison function:
01#include <string.h>
02#include <iostream>
03#include <map>
04#include <utility>
05 
06using namespace std;
07 
08struct cmp_str
09{
10   bool operator()(char const *a, char const *b)
11   {
12      return std::strcmp(a, b) < 0;
13   }
14};
15 
16int main()
17{
18   map<char *, int, cmp_str> Employees;
19 
20   // Examples of assigning Map container contents
21 
22   // 1) Assignment using array index notation
23   Employees["Mike C."] = 5234;
24   Employees["Charlie M."] = 3374;
25 
26   // 2) Assignment using member function insert() and STL pair
27   Employees.insert(std::pair<char *,int>("David D.",1923));
28  
29   // 3) Assignment using member function insert() and "value_type()"
30   Employees.insert(map<char *,int>::value_type("John A.",7582));
31 
32   // 4) Assignment using member function insert() and "make_pair()"
33   Employees.insert(std::make_pair((char *)"Peter Q.",5328));
34 
35   cout << "Map size: " << Employees.size() << endl;
36 
37   for( map<char *, int, cmp_str>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii)
38   {
39       cout << (*ii).first << ": " << (*ii).second << endl;
40   }
41}

Compile: g++ testMap.cpp
Run: ./a.out

Map size: 5 Charlie M.: 3374 David D.: 1923 John A.: 7582 Mike C.: 5234 Peter Q.: 5328 

SGI Map Documentation:


Example: STL multimap:

The STL multipmap allows duplicate keys.

01#include <string.h>
02#include <iostream>
03#include <map>
04#include <utility>
05 
06using namespace std;
07 
08int main()
09{
10  // Compare (<) function not required since it is built into string class.
11  // else declaration would comparison function in multimap definition.
12  // i.e. multimap<string, int, compare> m;
13 
14  multimap<string, int> m;
15 
16  m.insert(pair<string, int>("a", 1));
17  m.insert(pair<string, int>("c", 2));
18  m.insert(pair<string, int>("b", 3));
19  m.insert(pair<string, int>("b", 4));
20  m.insert(pair<string, int>("a", 5));
21  m.insert(pair<string, int>("b", 6));
22 
23  cout << "Number of elements with key a: " << m.count("a") << endl;
24  cout << "Number of elements with key b: " << m.count("b") << endl;
25  cout << "Number of elements with key c: " << m.count("c") << endl;
26 
27  cout << "Elements in m: " << endl;
28  for (multimap<string, int>::iterator it = m.begin();
29       it != m.end();
30       ++it)
31   {
32       cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
33   }
34 
35   pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp;
36 
37   // equal_range(b) returns pair<iterator,iterator> representing the range
38   // of element with key b
39   ppp = m.equal_range("b");
40 
41   // Loop through range of maps of key "b"
42   cout << endl << "Range of \"b\" elements:" << endl;
43   for (multimap<string, int>::iterator it2 = ppp.first;
44       it2 != ppp.second;
45       ++it2)
46   {
47       cout << "  [" << (*it2).first << ", " << (*it2).second << "]" << endl;
48   }
49 
50// Can't do this (??)
51//   cout << ppp.first << endl;
52//   cout << ppp.second << endl;
53 
54   m.clear();
55}

Compile: g++ testMultimap.cpp
Run: ./a.out

Number of elements with key a: 2
Number of elements with key b: 3
Number of elements with key c: 1
Elements in m:
  [a, 1]
  [a, 5]
  [b, 3]
  [b, 4]
  [b, 6]
  [c, 2]

Range of "b" elements:
  [b, 3]
  [b, 4]
  [b, 6]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值