首页 > 解决方案 > 为什么 map 的键不能通过迭代器插入到向量中?

问题描述

我想获取地图的键,保存在向量中,所以我编写了以下代码:

#include <vector>
#include <iostream>
#include <map>

using namespace std;

int main() {
  map<int, int> m = {{1,2}, {4,5}, {6,7}};
  vector<int> a(m.begin()->first, m.end()->first);
  for (auto i : a) {
    cout << i << endl;   // output 3 here
  }
}

此代码仅打印 3,这让我感到困惑。我从来没有在地图中输入 3,3 是从哪里出来的?

标签: c++std

解决方案


推荐阅读