关于修改map的值
  • 板块学术版
  • 楼主Wander_E
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/3/9 16:57
  • 上次更新2024/9/27 12:19:26
查看原帖
关于修改map的值
374347
Wander_E楼主2024/3/9 16:57

这段代码中两种修改方式只有第二种是正确的,这是为什么呢?

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
map<int, int> mp1, mp2;
int num1 = 0, num2 = 0;

signed main()
{
    mp1[1] = mp1[2] = 1;
    mp2[1] = mp2[2] = 1;
    for (auto it : mp1)
        it.second = ++num1;
    for (auto it : mp2)
        mp2[it.first] = ++num2;
    for (auto it : mp1)
        cout << it.second << ' ';
    cout << endl;
    for (auto it : mp2)
        cout << it.second << ' ';
    cout << endl;
    return 0;
}
2024/3/9 16:57
加载中...