std::map<std::string, int> myMap
时间: 2023-05-15 10:04:51 浏览: 64
这是一个 C++ 中的标准库容器,它是一个关联数组,可以将一个键映射到一个值。其中键的类型为 std::string,值的类型为 int。可以使用 myMap["key"] = value 的方式向其中添加元素,也可以使用 myMap.find("key") 的方式查找元素。
相关问题
写一个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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)