c++关于multiset的头文件包含问题

最近在Bilibili上看到不少侯捷老师C++的视频教程,侯捷老师翻译了很多C++的经典书籍,比如《Essential C++中文版》、《STL源码剖析》,也写了《深入浅出MFC 第二版》。
使用到multiset这个关联容器时,本来以为Visual Studio2017中会提供#include <multiset>这个头文件,没想到加入到显示错误,后来查了下资料,说是multiset只是set的一个特例而已,只需要包含set的头文件即可,使用#include<set>即可。
## 测试代码如下:
// test_multiset.hpp

c++关于multiset的头文件包含问题
 1 #ifndef _TEST_MULTISET_H
 2 #define _TEST_MULTISET_H
 3 
 4 #include <set>    // 注意:此处不能使用#include <multiset>
 5 #include <stdexcept>
 6 #include <string>
 7 #include <iostream>
 8 #include <ctime>
 9 #include <cstdio> // snprintf()
10 #include <cstdlib> // abort()
11 #include <algorithm>
12 
13 #include "pub.h"
14 
15 using std::multiset;
16 using std::cin;
17 using std::cout;
18 using std::endl;
19 using std::string;
20 using std::exception;
21 using std::find;
22 using std::sort;
23 
24 namespace jj06
25 {
26 void test_multiset(long& totalCount)
27 {
28 cout << "\ntest_multiset()......... \n";
29 
30 multiset<string> c;
31 char buf[10];
32 clock_t timeStart = clock();
33 for (long i = 0; i < totalCount; ++i)
34 {
35 try {
36 snprintf(buf, 10, "%d", rand() % 65535);
37 c.insert(string(buf));
38 }
39 catch (std::exception& e) {
40 
41 cout << "i=" << i << e.what() << endl;
42 abort();
43 }
44 }
45 
46 cout << "milli-seconds:" << (clock() - timeStart) << endl;
47 cout << "multiset.size()= " << c.size() << endl;
48 cout << "multiset.max_size()= " << c.max_size() << endl;
49 
50 string target = get_a_target_string();
51 {
52 timeStart = clock();
53 auto pItem = ::find(c.begin(), c.end(), target);    // 比c.find(...)慢很多
54 cout << "::find(), mill-seconds: " << (clock() - timeStart) << endl;
55 
56 if (pItem != c.end())
57 cout << "found, " << *pItem << endl;
58 else
59 cout << "not found! " << endl;
60 }
61 
62 {
63 timeStart = clock();
64 auto pItem = c.find(target);    // 比::find(...)快很多
65 cout << "::find(), mill-seconds: " << (clock() - timeStart) << endl;
66 
67 if (pItem != c.end())
68 cout << "found, " << *pItem << endl;
69 else
70 cout << "not found! " << endl;
71 }
72 }
73 }
74 #endif
View Code

 

// pub.h

c++关于multiset的头文件包含问题
 1 #ifndef _PUB_H_
 2 #define _PUB_H_
 3 
 4 #ifdef _MSC_VER
 5 #define snprintf _snprintf 
 6 #endif
 7 
 8 #include <string>
 9 #include <cstdio>
10 #include <iostream>
11 
12 using std::string;
13 using std::cin;
14 using std::cout;
15 
16 //const long ASIZE = 1000000;
17 const long ASIZE = 50000;
18 
19 long get_a_target_long()
20 {
21 long target = 0;
22 cout << "target (0~" << RAND_MAX << "): ";
23 cin >> target;
24 
25 return target;
26 }
27 
28 string get_a_target_string()
29 {
30 long target = 0;
31 char buf[10];
32 
33 cout << "target (0~" << RAND_MAX << "): ";
34 cin >> target;
35 snprintf(buf, 10, "%d", target);
36 
37 return string(buf);
38 }
39 int compareLongs(const void* a, const void* b)
40 {
41 return (*(long*)a - *(long*)b);
42 }
43 
44 int compareStrings(const void* a, const void* b)
45 {
46 if (*(string*)a > *(string*)b)
47 return 1;
48 else if(*(string*)a < *(string*)b)
49 return -1;
50 else
51 return 0;
52 }
53 #endif
View Code

 

// main.cpp

c++关于multiset的头文件包含问题
 1 #include "test_multiset.hpp"
 2 int main(int argc, char* argv[])
 3 {
 4 long totalCount;
 5 cout << "how many elements:";
 6 cin >> totalCount;
 7 
 8 srand((unsigned)time(NULL));
 9 jj06::test_multiset(totalCount);
10 
11 getchar();
12 
13 return 0;
14 }
View Code

 

## 运行示例

[root@192 src]# g++ -o test_multiset main.cpp pub.h test_multiset.hpp
[root@192 src]# ./test_multiset 
how many elements:1000000

test_multiset()......... 
milli-seconds:9750000
multiset.size()= 1000000
multiset.max_size()= 461168601842738790
target (0~2147483647): 23456
::find(), mill-seconds: 50000
found, 23456
c.find(), mill-seconds: 0
found, 23456
[root@192 src]# 、

 

## 完整的代码见本人的Github:

[stl_container_test](https://github.com/ccf19881030/stl_container_test)
### 附带侯捷老师B站的C++相关视频链接地址:
* [C++ STL与泛型编程高级-侯捷](https://www.bilibili.com/video/av48068999?p=6)
* [C++面向对象高级编程(侯捷)](https://www.bilibili.com/video/av27135524)
* [C++标准11-14](https://www.bilibili.com/video/av51795083)

上一篇:C++之set和multiset容器初学


下一篇:java-Json Multiset与ArrayList