c++關於multiset的頭文件包含問題

最近在Bilibili上看到很多侯捷老師C++的視頻教程,侯捷老師翻譯了不少C++的經典書籍,好比《Essential C++中文版》、《STL源碼剖析》,也寫了《深刻淺出MFC 第二版》。
C++ STL與泛型編程高級-侯捷
使用到multiset這個關聯容器時,原本覺得Visual Studio2017中會提供#include 這個頭文件,沒想到加入到顯示錯誤,後來查了下資料,說是multiset只是set的一個特例而已,只須要包含set的頭文件便可,使用#include便可。ios

測試代碼以下:

// test_multiset.hppgit

#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.hgithub

#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.cppweb

#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;
}

運行示例

[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

附帶侯捷老師B站的C++相關視頻連接地址:

本文同步分享在 博客「雪域迷影」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。shell

相關文章
相關標籤/搜索