相关疑难解决方法(0)

插入到std :: unordered_map在MSVC++的STL中调​​用哈希函数两次,设计不好还是特殊原因?

对于此代码:

#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)

c++ stl visual-c++ c++11

9
推荐指数
1
解决办法
435
查看次数

标签 统计

c++ ×1

c++11 ×1

stl ×1

visual-c++ ×1