std::map<std::string, int> m1 = {{"1", 1}}; std::map<std::string, int> m2 = {{"2", 2}}; std::map<std::string, int> m3 = {{"3", 3}}; std::map<std::string, int> m4 = {{"4", 4}}; std::vector<std::map<std::string, int>> v = {m1, m2, m3,m4}; // 将map中的数据存入vector中 std::vector<std::vector<std::map<std::string, int>>> c; for (int i = 0; i < v.size(); i += 2) { c.push_back({v[i], v[i + 1]}); }打印c里面的数据
时间: 2024-01-20 14:03:00 浏览: 33
可以使用以下代码打印出c里面的数据:
```
for (const auto& vec : c) {
for (const auto& m : vec) {
for (const auto& p : m) {
std::cout << p.first << ": " << p.second << " ";
}
std::cout << std::endl;
}
}
```
输出结果为:
```
1: 1
2: 2
3: 3
4: 4
```
相关问题
std::vector 存在3组std::map数据,分别是std::map<"1",2>,std::map<"2",3>,std::map<"3",1>想对它根据int数据从小到大排序,
好的,对于这个问题,可以按照以下步骤实现:
1. 将 `std::map` 中的数据存入一个 `std::vector` 中,这样可以方便排序。
2. 使用 `std::sort` 算法对 `std::vector` 进行排序,排序时需要自定义比较函数,按照 `std::map` 中的 `int` 数据从小到大排序。
下面是代码示例:
```cpp
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
bool map_cmp(const std::pair<std::string, int>& a, const std::pair<std::string, int>& b)
{
return a.second < b.second; // 按照map中的int数据从小到大排序
}
int main()
{
std::map<std::string, int> m1 = {{"1", 2}};
std::map<std::string, int> m2 = {{"2", 3}};
std::map<std::string, int> m3 = {{"3", 1}};
std::vector<std::map<std::string, int>> v = {m1, m2, m3}; // 将map中的数据存入vector中
std::cout << "排序前:\n";
for (auto& m : v) {
for (auto& p : m) {
std::cout << p.first << " " << p.second << "\n";
}
}
std::sort(v.begin(), v.end(), [](std::map<std::string, int>& a, std::map<std::string, int>& b) {
auto a_it = a.begin();
auto b_it = b.begin();
return a_it->second < b_it->second; // 按照map中的int数据从小到大排序
});
std::cout << "排序后:\n";
for (auto& m : v) {
for (auto& p : m) {
std::cout << p.first << " " << p.second << "\n";
}
}
return 0;
}
```
输出结果:
```
排序前:
1 2
2 3
3 1
排序后:
3 1
1 2
2 3
```
希望能对你有所帮助。
std::map初始化
std::map 是 C++ STL 中的一个关联容器,它提供了一种将键和值进行映射的方式。map 容器的初始化可以通过多种方式实现,下面是几个常用的方法:
1. 列表初始化
```c++
std::map<std::string, int> m {{"apple", 1}, {"banana", 2}, {"cherry", 3}};
```
2. 通过 std::make_pair() 初始化
```c++
std::map<std::string, int> m;
m.insert(std::make_pair("apple", 1));
m.insert(std::make_pair("banana", 2));
m.insert(std::make_pair("cherry", 3));
```
3. 使用迭代器初始化
```c++
std::map<std::string, int> m1 {{"apple", 1}, {"banana", 2}, {"cherry", 3}};
std::map<std::string, int> m2(m1.begin(), m1.end());
```
以上就是 std::map 的三种常用初始化方法。需要注意的是,map 容器中的元素是按照键的大小进行排序的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)