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

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

#include <set> // 注意:此处不能使用#include <multiset>
#include <stdexcept>
#include <string>
#include <iostream>
#include <ctime>
#include <cstdio> // snprintf()
#include <cstdlib> // abort()
#include <algorithm>

#include "pub.h"

using std::multiset;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::exception;
using std::find;
using std::sort;

namespace jj06
{
void test_multiset(long& totalCount)
{
cout << "\ntest_multiset()......... \n";

multiset<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < totalCount; ++i)
{
try {
snprintf(buf, 10, "%d", rand() % 65535);
c.insert(string(buf));
}
catch (std::exception& e) {

cout << "i=" << i << e.what() << endl;
abort();
}
}

cout << "milli-seconds:" << (clock() - timeStart) << endl;
cout << "multiset.size()= " << c.size() << endl;
cout << "multiset.max_size()= " << c.max_size() << endl;

string target = get_a_target_string();
{
timeStart = clock();
auto pItem = ::find(c.begin(), c.end(), target); // 比c.find(...)慢很多
cout << "::find(), mill-seconds: " << (clock() - timeStart) << endl;

if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}

{
timeStart = clock();
auto pItem = c.find(target); // 比::find(...)快很多
cout << "::find(), mill-seconds: " << (clock() - timeStart) << endl;

if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
}
}
#endif
```
// pub.h
```cpp
#ifndef _PUB_H_
#define _PUB_H_

#ifdef _MSC_VER
#define snprintf _snprintf
#endif

#include <string>
#include <cstdio>
#include <iostream>

using std::string;
using std::cin;
using std::cout;

//const long ASIZE = 1000000;
const long ASIZE = 50000;

long get_a_target_long()
{
long target = 0;
cout << "target (0~" << RAND_MAX << "): ";
cin >> target;

return target;
}

string get_a_target_string()
{
long target = 0;
char buf[10];

cout << "target (0~" << RAND_MAX << "): ";
cin >> target;
snprintf(buf, 10, "%d", target);

return string(buf);
}
int compareLongs(const void* a, const void* b)
{
return (*(long*)a - *(long*)b);
}

int compareStrings(const void* a, const void* b)
{
if (*(string*)a > *(string*)b)
return 1;
else if(*(string*)a < *(string*)b)
return -1;
else
return 0;
}
#endif
```

// main.cpp
```cpp

#include "test_multiset.hpp"
int main(int argc, char* argv[])
{
long totalCount;
cout << "how many elements:";
cin >> totalCount;

srand((unsigned)time(NULL));
jj06::test_multiset(totalCount);

getchar();

return 0;
}
```
## 运行示例
```shell
[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)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjjds.html