编译器如何将值写入此构造中该对的第二个值?

sti*_*ort 0 c++ stdmap std-pair

map<int,int> a;
pair<std::map<int,int>::iterator ,bool> f;
f=(a.insert({0,0}));
cout<<f.second;
Run Code Online (Sandbox Code Playgroud)

为什么输出1呢?

对于该对中的任何值,它始终输出 1

Ted*_*gmo 9

这是因为它bool f.second会告诉您是否insert将其插入pair<int,int>到地图中。1意味着它确实插入了它。

bools 通常打印为0( false) 或1( true)。您可以使用 I/O 操纵器std::boolalpha来打印truefalse代替。

对于该对中的任何值,它始终输出 1

不会。如果您尝试插入 a ,pair其中的Key值已存在于 中,map<int,int>它将返回 a,pair<std::map<int,int>::iterator ,bool>其中boolisfalse并且迭代器将指向 中的现有元素map<int,int>