Skip to content

败犬日报 2025-09-03

1. 哈希表怎么设计

像 C++ unordered_map 的链表法、java 的链表转红黑树,都走偏了。高性能哈希表的方向完全不是这样,在链表法上雕花显然不会增加访存局部性。

工程上哈希表的设计可以看群主文章:https://zclll.com/index.php/doris/hashtable_in_clickhouse.html

2. std::accumulate 的类型设计失误

cpp
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<double> a{1, 1.5};
    auto res = std::accumulate(a.begin(), a.end(), 0);
    std::cout << res << std::endl;  // 输出 2
}

std::accumulate 返回值的类型是那个初始值决定的,所以希望得到 double 的时候传了 0 就会寄。

ranges::fold_left 就很正确,它的返回值类型是 auto。都是左折叠,所以 std::accumulate 算设计失误。

这个坑太容易踩了。


为什么不要求和迭代器类型一致,可能是允许更灵活的操作,比如:

cpp
std::vector<char> v{'a', 'b', 'c'};
std::accumulate(v.begin(), v.end(), std::string{});