STL——容器(Map & multimap)的大小

1. Map & multimap 的大小

map.size();     //返回容器中元素的数目

map.empty();//判断容器是否为空, 容器中有内容将会返回 false

代码示例:

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1;
9 if (!mapStu1.empty())
10 {
11 cout << "容器 mapStu1 *有" << mapStu1.size() << "个元素" << endl;
12 }
13 else
14 {
15 cout << "容器 mapStu1 *有" << mapStu1.size() << "个元素, " << "容器为空" << endl;
16 }
17
18 cout << "插入元素" << endl;
19 mapStu1.insert(pair<int, string>(1, "内容A"));
20 mapStu1.insert(pair<int, string>(2, "内容B"));
21 mapStu1.insert(pair<int, string>(3, "内容C"));
22 mapStu1.insert(pair<int, string>(4, "内容D"));
23
24 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍历
25 {
26 cout << "mapStu1的第 " << it->first << "个参数为: " << it->second <<endl;
27 }
28
29 if (!mapStu1.empty()) //容器中有内容将会返回 false
30 {
31 cout << "容器 mapStu1 *有" << mapStu1.size() << "个元素" << endl;
32 }
33 else
34 {
35 cout << "容器 mapStu1 *有" << mapStu1.size() << "个元素, " << "容器为空" << endl;
36 }
37
38 return 0;
39 }

打印结果:

STL——容器(Map & multimap)的大小

=========================================================================================================================

上一篇:STL之六:map/multimap用法详解


下一篇:Java简单类(部门、领导、雇员关系)