关联性容器
底层为红黑树的关联式容器(红黑树:近似平衡的二插搜索树,保证最长路径不超过最短路径的两倍) (可遍历)
- 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;
}