unordered_map

链接:

https://www.sczyh30.com/posts/C-C/cpp-stl-hashmap/

https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/

http://www.cplusplus.com/reference/vector/vector/?kw=vector

https://zh.cppreference.com/w/cpp/container/unordered_map

内部unordered_map是使用Hash Table实现的,提供给map的键被散列为hash表的索引,这就是为什么数据结构的性能很大程度上取决于散列函数,但平均来说,搜索的成本,插入和从哈希表中删除的是O (1)

#include <iostream>
#include <unordered_map>

using namespace std;

int main(){
	//Declaring umap to be of <string, int> type
	//key will be of string type and mapped value will
	//be of double type
	std::unordered_map<string, int> umap;

	//inserting values by using [] operator
	umap["GeeksforGeeks"] = 10;
	umap["Practice"] = 20;
	umap["Contribute"] = 30;

	//Traversing an unordered map
	for(auto x : umap){
		cout<<x.first<<" "<<x.second<<endl;
	}
}

unordered_map vs unordered_set
在unordered_set中,我们只有key,没有值,这些主要用于查看集合中的存在/不存在。例如,考虑计算单个单词的频率的问题。我们不能使用unordered_set(或set),因为我们无法存储计数

unordered_map vs map
map(like set)是唯一键的有序序列,而unordered_map键可以按任何顺序存储,因此无序。
Map被实现为平衡树结构,这就是为什么可以维持元素之间的顺序(通过特定的树遍历)。映射操作的时间复杂度为O(Log n),而对于unordered_set,它平均为O(1)。

unordered_map的成员函数,最常见的是 operator=, operator[], 容量是否为空或大小,迭代器的开始和结束,查找、计数、插入和擦除。C++11支持的标准库提供函数可以查看内部使用的桶数,桶大小以及使用的散列函数和各种散列策略的功能,但是在实际中不太有用。

#include <iostream>
#include <unordered_map>

using namespace std;

int main(){
	//Declaring umap to be of <string, string> type
	//key will be of string type and mapped value will
	//be of double type
	unordered_map<string, double> umap;

	//inserting values by using [] operator
	umap["PI"] = 3.14;
	umap["root2"] = 1.414;
	umap["root3"] = 1.732;
	umap["log10"] = 2.302;
	umap["loge"]  = 1.0;

	//inserting value by insert function
	umap.insert(make_pair("e", 2.718));

	string key = "PI";

	//if key not found in map iterator to end is returned
	if(umap.find(key) == umap.end()){
		cout<<key<<"not found\n\n";
	}
	//if key found then iterator to that key is returned
	else{
		cout<<"found"<<key<<endl;
	}

	//iterator over all value of umap
	unordered_map<string, double>::iterator itr;
	cout<<"\nAll Elements: \n";
	for(itr = umap.begin(); itr != umap.end(); itr++){
		//itr works as a pointer to pair<string, double>
		//type itr->first stores the key part and
		//itr->second stroes the value part
		cout<< itr->first<< " "<<itr->second<<endl;
	}
	return 0;
}
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

void printFrequencies(const string &str){
	unordered_map<string, int> wordFreq;
	stringstream ss(str); //Used for breaking words
	string word;
	 while(ss >> word){
		wordFreq[word]++;
	 }
	 unordered_map<string, int>::iterator p;
	 for(p = wordFreq.begin(); p != wordFreq.end(); p++){
		 cout<<"("<<p->first<<","<<p->second<<")\n";
	 }
}

int main(){
	string str = "geeks for geeks quiz "
				 "practice qa for";
	//给定一串单词找出单个单词的频率
	printFrequencies(str);
	return 0;
}
  • at():C ++ unordered_map中的此函数返回对元素为键k的值的引用。
  • begin():返回指向unordered_map容器中容器中第一个元素的迭代器
  • end():返回一个迭代器,指向unordered_map容器中容器中最后一个元素之后的位置
  • bucket()返回具有键k的元素位于映射中的bucket编号。
  • bucket_count bucket_count用于计算总数。unordered_map中的桶数。传递给此函数不需要参数。
  • bucket_size返回unordered_map的每个桶中的元素数。

std::unordered_map::operator[]

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

int main()
{
    std::unordered_map<char, int> letter_counts {{'a', 27}, {'b', 3}, {'c', 1}};

    std::cout << "initially:\n";
    for (const auto &pair : letter_counts) {
        std::cout << pair.first << ": " << pair.second << '\n';
    }
    letter_counts['b'] = 42;  // 更新既存值
    letter_counts['x'] = 9;  // 插入新值
    std::cout << "after modifications:\n";
    for (const auto &pair : letter_counts) {
        std::cout << pair.first << ": " << pair.second << '\n';
    }
    // 统计每个词的出现数
    // (首次调用 operator[] 以零初始化计数器)
    std::unordered_map<std::string, size_t>  word_map;
    for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
                           "this", "sentence", "is", "a", "hoax"}) {
        ++word_map[w];
    }

    for (const auto &pair : word_map) {
        std::cout << pair.second << " occurrences of word '" << pair.first << "'\n";
    }
}

operator ==,!=(std::unordered_map) 

https://zh.cppreference.com/w/cpp/container/unordered_map/operator_cmp

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值