C++ - 数据容器之 unordered_map(声明与初始化、插入元素、访问元素、遍历元素、删除元素、查找元素)

发布于:2025-04-11 ⋅ 阅读:(37) ⋅ 点赞:(0)

一、unordered_map

  • unordered_map 是 C++ STL 中的一个关联容器,它有如下特点
  1. unordered_map 存储键值对,使用哈希表实现

  2. unordered_map 的每个键在容器中只能出现一次

  3. unordered_map 的存储的键值对是无序的

  4. 平均情况下,查找、插入、删除都是 O(1) 时间复杂度


二、声明与初始化

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

	// 空 unordered_map
	unordered_map<string, int> map1;

	// 初始化 unordered_map
	unordered_map<string, int> map2 = {
		{"Alice", 25},
		{"Bob", 30} };

	return 0;
}

三、插入元素

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

	unordered_map<string, int> wordCount;

	// 使用 insert 方法
	wordCount.insert({ "apple", 3 });

	// 使用 operator[]
	wordCount["banana"] = 2;

	// 使用 emplace 方法
	wordCount.emplace("cherry", 5);

	return 0;
}

四、访问元素

1、演示
#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

	unordered_map<string, int> map = {
		{"Alice", 25},
		{"Bob", 30} };

	// 使用 operator[]
	cout << map["Alice"] << endl;
	cout << map["Bob"] << endl;

	// 使用 at 方法
	try {
		int result = map.at("Tom");
	}
	catch (out_of_range& e) {
		cout << "Key not found" << endl;
	}

	return 0;
}
# 输出结果

25
30
Key not found
2、注意事项
  • 使用 operator[] 访问,如果元素不存在,会创建新元素
#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

	unordered_map<string, int> map = {
		{"Alice", 25},
		{"Bob", 30} };


	cout << "unordered_map 大小为:" << map.size() << endl;
	cout << map["Tom"] << endl;
	cout << "unordered_map 大小为:" << map.size() << endl;

	return 0;
}
# 输出结果

unordered_map 大小为:2
0
unordered_map 大小为:3

五、遍历元素

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

	unordered_map<string, int> map = {
		{"Alice", 25},
		{"Bob", 30} };

	// 使用迭代器
	for (auto it = map.begin(); it != map.end(); it++) {
		cout << it->first << ": " << it->second << endl;
	}

	cout << "----------" << endl;

	// 使用范围 for 循环
	for (auto& it : map) {
		cout << it.first << ": " << it.second << endl;
	}

	return 0;
}
# 输出结果

Alice: 25
Bob: 30
----------
Alice: 25
Bob: 30

六、删除元素

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

    unordered_map<string, int> map = {
        {"Alice", 25},
        {"Bob", 30},
		{"Tom", 35} };

    cout << "unordered_map 大小为:" << map.size() << endl;

    // 通过键删除
	map.erase("Bob");
	cout << "erase 操作之后,unordered_map 大小为:" << map.size() << endl;

	// 全部删除
	map.clear();
	cout << "clear 操作之后,unordered_map 大小为:" << map.size() << endl;

    return 0;
}
# 输出结果

unordered_map 大小为:3
erase 操作之后,unordered_map 大小为:2
clear 操作之后,unordered_map 大小为:0

七、查找元素

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{

    unordered_map<string, int> map = {
        {"Alice", 25},
        {"Bob", 30} };

    auto it1 = map.find("Alice");
    if (it1 != map.end())
    {
        cout << it1->second << endl;
    }
    else {
        cout << "Key not found" << endl;
    }

    auto it2 = map.find("Tom");
    if (it2 != map.end())
    {
        cout << it2->second << endl;
    }
    else {
        cout << "Key not found" << endl;
    }

    return 0;
}
# 输出结果

25
Key not found

网站公告

今日签到

点亮在社区的每一天
去签到