std::map<std::string, int>()
时间: 2024-03-04 12:46:52 浏览: 30
std::map<std::string, int> 是C++标准库中一个关联容器,它提供了一种将键值对进行存储和访问的方式。在这个容器中,每个键都是唯一的,并且按照键的顺序进行排序。
这个容器的模板参数是两个类型,分别是键的类型和值的类型。在这个例子中,键的类型是 std::string,值的类型是 int。
使用 std::map 可以实现类似字典的功能,通过键来查找对应的值。它内部使用红黑树(一种自平衡二叉搜索树)来实现高效的插入、删除和查找操作。
下面是一些 std::map<std::string, int> 的常用操作:
1. 插入元素:可以使用 insert() 函数或者使用下标操作符 [] 来插入键值对。
2. 删除元素:可以使用 erase() 函数来删除指定键的元素。
3. 查找元素:可以使用 find() 函数来查找指定键的元素。
4. 访问元素:可以使用下标操作符 [] 来访问指定键对应的值。
相关问题
写一个std::map<std::string, std::variant<int, std::string>>赋值并读取的demo
当使用`std::map`结合`std::variant`在C++中存储键值对时,你可以创建一个映射,其中键是`std::string`类型,值是一个可以是`int`或`std::string`类型的`std::variant`。以下是一个简单的示例,展示如何创建、添加、访问和读取这些数据:
```cpp
#include <iostream>
#include <map>
#include <variant>
#include <string>
using Value = std::variant<int, std::string>;
// 创建一个std::map实例
std::map<std::string, Value> myMap;
// 添加元素
void addElement(const std::string& key, int intValue) {
myMap[key] = intValue;
std::cout << "Added " << key << " with int value: " << intValue << std::endl;
}
void addElement(const std::string& key, const std::string& stringValue) {
myMap[key] = stringValue;
std::cout << "Added " << key << " with string value: " << stringValue << std::endl;
}
// 读取元素(假设值是int)
int getValueAsInt(const std::string& key) {
auto it = myMap.find(key);
if (it != myMap.end()) {
return std::get<int>(*it->second);
}
throw std::runtime_error("Key not found in the map");
}
// 读取元素(假设值是std::string)
std::string getValueAsString(const std::string& key) {
auto it = myMap.find(key);
if (it != myMap.end()) {
return std::get<std::string>(*it->second);
}
throw std::runtime_error("Key not found in the map");
}
int main() {
// 示例用法
addElement("key1", 100);
addElement("key2", "Hello");
// 读取并输出int值
try {
std::cout << "Value of key1 as int: " << getValueAsInt("key1") << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
// 读取并输出string值
try {
std::cout << "Value of key2 as string: " << getValueAsString("key2") << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
```
在这个例子中,`addElement`函数用于向map中添加元素,`getValueAsInt`和`getValueAsString`分别用于根据键获取对应的整数或字符串值。注意,由于`std::variant`的值可能包含不同类型的值,因此在读取时需要指定类型进行解包。
std::map<std::string, std::map<std::string, uint32_t>>
`std::map<std::string, std::map<std::string, uint32_t>>` 是一个C++中的嵌套映射容器,它可以用来存储键值对的集合。在这个例子中,外层的`std::map`使用`std::string`作为键,内层的`std::map`使用`std::string`作为键,`uint32_t`作为值。
以下是一个示例代码,演示了如何使用`std::map<std::string, std::map<std::string, uint32_t>>`来存储和访问数据:
```cpp
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, std::map<std::string, uint32_t>> myMap;
// 添加数据
myMap["category1"]["item1"] = 10;
myMap["category1"]["item2"] = 20;
myMap["category2"]["item1"] = 30;
// 访问数据
std::cout << "Value of category1.item1: " << myMap["category1"]["item1"] << std::endl;
std::cout << "Value of category2.item1: " << myMap["category2"]["item1"] << std::endl;
return 0;
}
```
输出结果:
```
Value of category1.item1: 10
Value of category2.item1: 30
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)