c++哈希表的初始化
时间: 2024-06-18 17:03:17 浏览: 17
C++中的哈希表是一种用于快速查找的数据结构,其初始化步骤如下:
1. 头文件:需要包含<unordered_map>头文件。
2. 定义哈希表:使用unordered_map容器定义一个哈希表,比如:
unordered_map<int, string> myMap;
3. 插入元素:使用insert()函数向哈希表中插入元素,比如:
myMap.insert(make_pair(1, "apple"));
myMap.insert(make_pair(2, "banana"));
myMap.insert(make_pair(3, "orange"));
4. 访问元素:使用at()函数或[]操作符来访问哈希表中的元素,比如:
cout << myMap.at(1) << endl; // 输出 "apple"
cout << myMap << endl; // 输出 "banana"
5. 删除元素:使用erase()函数来删除哈希表中的元素,比如:
myMap.erase(3);
相关问题
初始化哈希表
在C++中,可以使用`unordered_map`模板类来实现哈希表。`unordered_map`中的每个元素都是一个键值对(key-value pair),其中键(key)用于索引值(value),可以是任何类型的数据,而值(value)则是与键相关联的数据,也可以是任何类型的数据。
以下是使用`unordered_map`初始化哈希表的示例代码:
```c++
#include <unordered_map>
#include <string>
#include <iostream>
int main() {
std::unordered_map<std::string, int> my_map = {
{"apple", 5},
{"banana", 3},
{"cherry", 9}
};
std::cout << "Size of my_map: " << my_map.size() << std::endl;
std::cout << "Value of apple: " << my_map["apple"] << std::endl;
std::cout << "Value of banana: " << my_map["banana"] << std::endl;
std::cout << "Value of cherry: " << my_map["cherry"] << std::endl;
return 0;
}
```
输出结果为:
```
Size of my_map: 3
Value of apple: 5
Value of banana: 3
Value of cherry: 9
```
在上面的代码中,我们首先引入了`unordered_map`、`string`和`iostream`头文件。然后,我们定义了一个`unordered_map`对象`my_map`,它的键类型是`string`,值类型是`int`。我们使用大括号初始化列表来初始化`my_map`对象,其中每个键值对都用逗号分隔开,每个键值对包含一个键和一个值,用冒号分隔开。
在输出结果中,我们首先输出了`my_map`的大小(即键值对的数量),然后分别输出了三个键(`"apple"`、`"banana"`和`"cherry"`)对应的值。
需要注意的是,当使用`unordered_map`的`operator[]`操作符访问一个不存在的键时,会自动创建该键,并将其对应的值初始化为默认值(`0`、`false`、`nullptr`等)。如果需要避免这种行为,可以使用`unordered_map`的`find()`函数来查找键是否存在。
c++哈希表count函数
哈希表是一种数据结构,它可以通过散列函数将键映射到存储位置。在C语言中,哈希表通常是使用数组和链表的组合来实现的。
如果你要统计哈希表中某个元素的出现次数,你可以使用count函数来实现。下面是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
typedef struct Node {
char key[20];
int value;
struct Node* next;
} Node;
Node* hashTable[SIZE];
// 初始化哈希表
void initializeHashTable() {
for (int i = 0; i < SIZE; ++i) {
hashTable[i] = NULL;
}
}
// 散列函数
int hashFunction(const char* key) {
int sum = 0;
for (int i = 0; i < strlen(key); ++i) {
sum += key[i];
}
return sum % SIZE;
}
// 向哈希表中插入新节点
void insert(const char* key, int value) {
int index = hashFunction(key);
Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->key, key);
newNode->value = value;
newNode->next = NULL;
if (hashTable[index] == NULL) {
hashTable[index] = newNode;
} else {
Node* currentNode = hashTable[index];
while (currentNode->next != NULL) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
}
// 统计元素出现次数
int count(const char* key) {
int index = hashFunction(key);
if (hashTable[index] == NULL) {
return 0;
} else {
int count = 0;
Node* currentNode = hashTable[index];
while (currentNode != NULL) {
if (strcmp(currentNode->key, key) == 0) {
count++;
}
currentNode = currentNode->next;
}
return count;
}
}
int main() {
initializeHashTable();
insert("apple", 1);
insert("banana", 2);
insert("apple", 3);
printf("Count of 'apple': %d\n", count("apple"));
printf("Count of 'banana': %d\n", count("banana"));
return 0;
}
```
在上面的示例代码中,我们定义了一个哈希表结构体`Node`,以及一些相关的操作函数。其中`count`函数用于统计指定键的出现次数。在`main`函数中,我们插入了一些数据,并使用`count`函数来获取指定键的出现次数。输出结果为:
```
Count of 'apple': 2
Count of 'banana': 1
```
希望这个示例代码对你有帮助!如果你有任何问题,请随时提问。