【问题标题】:Searching a in multimap for a range of keys在多图中搜索一系列键
【发布时间】:2019-08-25 10:12:59
【问题描述】:

我正在编写一个大学图书馆系统。 作为书籍\杂志数据库,我使用了一个关键是项目 ID 号的地图 (1000000 - 99999999),值是指向项目对象的指针。

每个项目都与拼贴画上的 7 个现有学院之一相关,这就是项目密钥的设置方式。 第一个数字是教员编号( 1 -7),其余数字是 0 000000X(当 x 是相对于项目大小的数字时)

我的问题是我可以得到多少个键与数字说明 (1 - 7) 所以我会知道特定学院有多少人。

【问题讨论】:

  • 听起来像是 std::equal_range 的工作。
  • 在不改变结构的情况下,没有比 O(N) 更快的解决方案。如果你有一个排序的向量而不是一个地图,你可以使用二进制搜索来获得 O(logN) 的解决方案。或者添加另一个以第一个数字为键的多图。
  • "第一个数字是教职号 (1 -7)" - 但是 ID 号的范围是[1000000, 99999999],那么 ID 号以 8 开头或者9?

标签: c++ c++11 visual-c++ c++14


【解决方案1】:

也许我在这里错过了一些要点。但通常你可以简单地使用std::count_if 来计算。

希望我正确理解了你的问题。

这是许多可能的解决方案之一:

#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <iostream>

struct TestItem
{
    int id{ 0 };
} testItemObject;

int main()
{
    // Define a multimap with testdata  
    std::multimap<std::string, TestItem*> testMM{ {"1000000", &testItemObject},
        {"2000000", &testItemObject},{"2000001", &testItemObject},
        {"3000000", &testItemObject},{"3000001", &testItemObject},{"3000002", &testItemObject},
        {"4000000", &testItemObject},{"4000001", &testItemObject},{"4000002", &testItemObject},{"4000003", &testItemObject},
        {"5000000", &testItemObject},{"5000001", &testItemObject},{"5000002", &testItemObject},{"5000003", &testItemObject},{"5000004", &testItemObject},
        {"6000000", &testItemObject},{"6000001", &testItemObject},{"6000002", &testItemObject},{"6000003", &testItemObject},{"6000004", &testItemObject},{"6000005", &testItemObject},
        {"7000000", &testItemObject},{"7000001", &testItemObject},{"7000002", &testItemObject},{"7000003", &testItemObject},{"7000004", &testItemObject},{"7000005", &testItemObject},{"7000006", &testItemObject},
        {"8000000", &testItemObject},{"8000001", &testItemObject},{"8000002", &testItemObject},{"8000003", &testItemObject},{"8000004", &testItemObject},{"8000005", &testItemObject},{"8000006", &testItemObject},{"8000007", &testItemObject},
        {"9000000", &testItemObject},{"9000001", &testItemObject},{"9000004", &testItemObject},{"9000003", &testItemObject},{"9000004", &testItemObject},{"9000005", &testItemObject},{"9000006", &testItemObject},{"9000007", &testItemObject},{"9000008", &testItemObject},
    };

    // We want to look for keys starting with
    std::vector<char> firstLetterToSearch{ '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    // Using std::count_if to count
    std::for_each(firstLetterToSearch.begin(), firstLetterToSearch.end(),
        [&testMM](const char c) { 
            std::cout << c << " --> " << 
                std::count_if(testMM.begin(), testMM.end(), [&c](std::pair<const std::string, TestItem*> & mm) { return mm.first[0] == c; }) 
                << "\n"; 
        }
    );

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多