对于此代码:
#include<unordered_map>
#include<iostream>
using namespace std;
struct myhash {
unsigned operator()(const unsigned&v)const {
cout<<"Hash function is called:"<<v<<endl;
return v;
}
};
unordered_map<unsigned,unsigned,myhash>mp;
int main() {
for (unsigned i=0;i<3;++i) {
cout<<"Visiting hash table:"<<i<<endl;
++mp[i];
}
}
Run Code Online (Sandbox Code Playgroud)
使用g ++时,输出并不奇怪:
Visiting hash table:0
Hash function is called:0
Visiting hash table:1
Hash function is called:1
Visiting hash table:2
Hash function is called:2
Run Code Online (Sandbox Code Playgroud)
但MSVC++(2015)的输出震惊了我:
Visiting hash table:0
Hash function is called:0
Hash function is called:0
Visiting hash table:1
Hash function is called:1
Hash function is …Run Code Online (Sandbox Code Playgroud)