c++ - 有序插入到 STL::map 更快?

标签 c++ stl containers

因为 STL::map 是有序映射,插入排序数据集会更快吗?特别是如果我们考虑大型数据集?

最佳答案

当然,已经对数据进行排序。

#include <map>
#include <vector>

int main() {
    std::vector<int> data { 0, 1, 2, 3, 4, 5 };
    std::map<int, int> result;
    // From: http://en.cppreference.com/w/cpp/container/map/insert
    // Amortized constant if the insertion happens in the position just before
    // the hint, logarithmic in the size of the container otherwise.
    for(auto i : data)
        result.insert(result.end(), { i, i} );
}

关于c++ - 有序插入到 STL::map 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39751518/

相关文章:

docker - 使用 Jenkinsfile 让容器运行

c++ - 在细胞中产生噪声 - CPP 和 GLSL

c++ - 你如何在 win32 C++ 中写入 xml 文件?

c++ - 带有 cpp 的 ndk 中的标准 - vector 问题

c++ - 返回 STL 列表作为参数

Docker:runc:[2:INIT] 是什么意思?

c++ - boost flat_map批量插入

c++ - 搜索对应的对

c++ - 在 C++ 中相互交换数组的部分

c++ - 调整 STL vector 的大小是否会删除/使其以前的内容无效?