举一个std::unordered_map的key为const类型的例子
时间: 2023-09-12 13:05:20 浏览: 97
### 回答1:
以下是一个示例,其中std::unordered_map的key类型为const std::string,值类型为int:
```
#include <unordered_map>
#include <iostream>
#include <string>
int main() {
std::unordered_map<const std::string, int> myMap = { {"apple", 1}, {"banana", 2}, {"orange", 3} };
// 输出map中的所有键值对
for(const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// 查找某个键的值
const std::string key = "banana";
auto it = myMap.find(key);
if(it != myMap.end()) {
std::cout << "The value for key '" << key << "' is " << it->second << std::endl;
}
return 0;
}
```
在上面的示例中,我们创建了一个std::unordered_map对象myMap,其键类型为const std::string,值类型为int。我们使用大括号语法初始化了这个map,其中键是字符串常量,值是整数。
我们可以使用for循环输出map中的所有键值对,并使用find函数查找特定键的值。在这个例子中,我们搜索了一个名为"banana"的键,并输出了它对应的值2。
### 回答2:
假设我们有一个存储学生信息的数据库,每个学生有个唯一的学号(student ID)作为其标识。我们可以使用`std::unordered_map`来实现这个数据库,其中key的类型为`const int`。
```cpp
#include <iostream>
#include <unordered_map>
struct Student {
std::string name;
int age;
};
int main() {
std::unordered_map<const int, Student> studentDB;
// 添加学生信息到数据库
Student s1 = {"Alice", 20};
studentDB[1001] = s1;
Student s2 = {"Bob", 21};
studentDB[1002] = s2;
// 根据学号查询学生信息
int studentID = 1001;
auto it = studentDB.find(studentID);
if (it != studentDB.end()) {
std::cout << "Student ID: " << it->first << std::endl;
std::cout << "Name: " << it->second.name << std::endl;
std::cout << "Age: " << it->second.age << std::endl;
} else {
std::cout << "Student not found!" << std::endl;
}
return 0;
}
```
这个例子中,我们通过使用`std::unordered_map<const int, Student>`创建了一个存储学生信息的数据库。每个学生有一个唯一的`const int`类型的学号作为其标识。我们通过将学生信息作为value存储在unordered_map中,并使用学号作为key来进行索引和查询特定的学生信息。
在这个例子中,我们使用学号1001和1002来标识两个学生的信息并添加到数据库中。然后,我们可以通过查询学号来获取对应学生信息。通过这样的方式,我们可以方便地使用学号作为key来快速访问和修改对应学生的其他信息。同时,由于学号是`const`类型,保证了学号在添加进数据库后不会被修改。
### 回答3:
std::unordered_map 是 C++ 中的一个容器类型,它提供了快速的查找功能,允许使用自定义的 Key 和 Value 对进行存储和访问。
对于 std::unordered_map 的 Key 类型可以是任何可哈希的类型,包括内置类型(如整数、浮点数等)、自定义结构体或类等。
在这里,我们举一个 key 为 const 类型的例子。假设我们想要创建一个存储学生信息的 std::unordered_map,其中 key 是学生的学号,类型为 const int,value 是学生的姓名,类型为 std::string。
代码示例如下:
```
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<const int, std::string> studentMap;
// 向 unordered_map 中插入学生信息
studentMap.insert({10001, "Alice"});
studentMap.insert({10002, "Bob"});
studentMap.insert({10003, "Charlie"});
// 查找特定学号对应的学生姓名
const int targetStudentId = 10002;
auto it = studentMap.find(targetStudentId);
if (it != studentMap.end()) {
std::cout << "学号" << targetStudentId << "对应的学生姓名是:" << it->second << std::endl;
} else {
std::cout << "没有找到学号" << targetStudentId << "对应的学生信息" << std::endl;
}
return 0;
}
```
在上述示例中,我们创建了一个 std::unordered_map,键的类型为 const int,值的类型为 std::string。我们插入了几个学生的学号和对应的姓名信息,并使用 find 方法查找指定学号的学生姓名,并输出结果。
需要注意的是,由于 std::unordered_map 的键类型是 const int,因此无法修改键的值,这也是 const 类型的特性所限制的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)