关于 STL map

灌水区

Shunpower @ 2021-11-13 17:25:40

RT,string s,map.erase(s)是删除了 map[s],还是map[s->first]


by 仙山有茗 @ 2021-11-13 17:29:00

@LEMON_ni 应该是把map[s->first]清零了罢,但是内部我不清楚


by Shunpower @ 2021-11-13 17:30:31

@LYqwq

如果我map.erase(it)之后map.count(it->first)会是 0


by 仙山有茗 @ 2021-11-13 17:31:50

@LEMON_ni 是的,map所有元素默认值为0,可以用这个程序康康:

#include <iostream>
#include <map>
using namespace std;
map<string,int> m;

int main(){
    string s="kkksc03";
    m[s]+=3;
    cout << m[s] << endl;
    m.erase(s);
    cout << m[s] << endl;
    cout << m["23333"] << endl;
    return 0;
}

输出为:

3
0
0

by Shunpower @ 2021-11-13 17:32:44

@LYqwq

thx


|