侯捷-STL与泛型编程笔记(第一讲、容器概述——3.关联式容器)

关联性容器

底层为红黑树的关联式容器(红黑树:近似平衡的二插搜索树,保证最长路径不超过最短路径的两倍) (可遍历)

  • Map valeu不一定等于key
  • Set value=key
  • multimap(multi代表可重复)
  • multiset

底层为哈希的关联式容器:无序 (无序,无遍历元素)
unordered_multiset(相同元素放同一个篮子)
多个篮子组合成,一个篮子存放一个链表(篮子数要大于元素数量,因为元素大于或等于篮子数,篮子扩充2倍)

  • unorder_map
  • unorder_multimap
  • unorder_set
  • unorder_multiset

应用场景:

  • (1)如果需要有序的关联式容器—>map
  • (2) 与是否有序无关—>查找效率高,占用内存大->underedmap

知识点一:rand(函数范围为0-32767
知识点二:分配器allocator用和释放都要加大小,malloc和free、new和delete不需要传入大小

multiset

#include <set>
#include <stdexcept>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>

void test_multiset(const long &value)
{
	multiset<string> c;
	char buf[10];
	clock_t timeStart = clock();
	for (long i = 0; i < value; ++i)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());
			c.insert(string(buf));
		}
		catch (exception &p)
		{
			cout << "i=" << i << " " << p.what() << endl;
			abort();
		}
	}

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

	string target = get_a_target_string();
	{
		timeStart = clock();
		auto pItem = find(c.begin(), c.end(), target);  //比 c.find(...) 慢很多	
		cout << "std::find(), milli-seconds:" << (clock() - timeStart) << endl;
		if (pItem != c.end())
			cout << "found, " << *pItem << endl;
		else
			cout << "not found!" << endl;
	}

	{
		timeStart = clock();
		auto pItem = c.find(target);  //比 std::find(...) 快很多							
		cout << "c.find(), milli-seconds:" << (clock() - timeStart) << endl;
		if (pItem != c.end())
			cout << "found, " << *pItem << endl;
		else
			cout << "not found!" << endl;
	}
	c.clear();
}

在这里插入图片描述

multimap

#include <map>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>

void test_multimap(const long &value)
{
	multimap<long, string> c;
	char buf[10];
	clock_t timeStart = clock();
	for (long i = 0; i < value; ++i)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());
			//multimap不可使用[]做insertion
			c.insert(pair<long, string>(i, buf));
		}
		catch(exception &p)
		{
			cout << "i=" << i << " " << p.what() << endl;
			abort();
		}
	}
	cout << "milli-seconds:" << (clock() - timeStart) << endl;  
	cout << "multimap.size()=" << c.size() << endl;
	cout << "multima.max_size()=" << c.max_size() << endl;

	long target = get_a_target_long();
	timeStart = clock();
	auto pItem = c.find(target);  
	if (pItem != c.end())
		cout << "found, milli-seconds: " << (clock() - timeStart) << endl;
	else
		cout << "not found!" << endl;

	c.clear();

}

在这里插入图片描述

unordered_multiset

#include <unordered_set>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>

void test_unordered_multiset(const long &value)
{
	unordered_multiset<string> c;
	char buf[10];

	clock_t timeStart = clock();
	for (long i = 0; i < value; ++i)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());
			c.insert(string(buf));
		}
		catch (exception &p)
		{
			cout << "i=" << i << " " << p.what() << endl;
			abort();
		}
	}
	cout << "milli-seconds:" << (clock() - timeStart) << endl;
	cout << "unordered_multiset.size()=" << c.size() << endl;
	cout << "unordered_multiset.max_size()=" << c.max_size() << endl;
	cout << "unordered_multiset.bucket_count()=" << c.bucket_count() << endl;
	cout << "unordered_multiset.load_factor()=" << c.load_factor() << endl;
	cout << "unordered_multiset.max_load_factor()=" << c.max_load_factor() << endl;
	cout << "unordered_multiset.max_bucket_count()=" << c.max_bucket_count() << endl;
	for (unsigned i = 0; i < 20; ++i)
		cout << "bucket #" << i << "has " << c.bucket_size(i) << "elemets.\n";

	string target = get_a_target_string();
	{
		timeStart = clock();
		auto pItem = find(c.begin(), c.end(), target);
		cout << "std::find(), milli-seconds:" << (clock() - timeStart) << endl;
		if (pItem != c.end())
			cout << "found, " << *pItem << endl;
		else
			cout << "not found!" << endl;
	}

	{
		timeStart = clock();
		auto pItem = c.find(target);
		cout << "c.find(), milli-seconds: " << (clock() - timeStart) << endl;
		if (pItem != c.end())
			cout << "found, " << *pItem << endl;
		else
			cout << "not found!" << endl;
	}
	c.clear();
}

在这里插入图片描述

unordered_multimap

#include <unordered_map>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>

void test_unordered_multimap(const long&value)
{
	unordered_multimap<long, string> c;
	char buf[10];
	clock_t timeStart = clock();
	for (long i = 0; i < value; ++i)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());
			c.insert(pair<long, string>(i, buf));
		}
		catch (exception &p)
		{
			cout << "i=" << i << " " << p.what() << endl;
			abort();
		}
	}
	cout << "milli-seconds:" << (clock() - timeStart) << endl;
	cout << "unodered_multimap.size()=" << c.size() << endl;
	cout << "unordered_multimap.max_size()=" << c.max_size() << endl;
	long target = get_a_target_long();
	timeStart = clock();
	auto pItem = c.find(target);
	cout << "c.find(), milli-seconds : " << (clock() - timeStart) << endl;
	if (pItem != c.end())
		cout << "found, value=" << (*pItem).second << endl;
	else
		cout << "not found! " << endl;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值