second;//it->first将是键/位置 std::cout,c++,hash,C++,Hash" /> second;//it->first将是键/位置 std::cout,c++,hash,C++,Hash" />

如何在c+;中使用哈希在同一个键下存储多个值+; 我想用Hash技术在C++中建立一个电话簿项目。我需要做的是它必须根据位置搜索联系人。所以我将把位置作为键值,但是在同一个位置会有多个联系人。因此,如何在同一个键(位置)上存储多个值(姓名,电话号码)。。它是一个允许使用相同密钥的多个条目的哈希映射。要为每个条目存储多个属性,可以使用结构或类。最后,它可能是这样的: struct contact_t { std::string name; std::string phone_number; } std::unordered_multimap<std::string, contact_t> directory; int main(int argc, char *argv[]) { // Add entry contact_t contact; contact.name = "Fooman"; contact.phone_number = "00 000 00000000"; directory.emplace("Barcity", contact); // List all entries of one city auto range = directory.equal_range("Barcity"); for (auto it = range.first; it != range.second; ++it) { auto &entry = it->second; // it->first would be the key/location std::cout << "Name: " << entry.name << "Phone: " << entry.phone_number << std::endl; } } struct contact\t{ std::字符串名; std::字符串电话号码; } std::无序的_多映射目录; int main(int argc,char*argv[]) { //添加条目 联系方式; contact.name=“Fooman”; contact.phone_number=“00 000 00000000”; 安置目录(“酒吧城”,联系人); //列出一个城市的所有条目 自动范围=目录。相等范围(“Barcity”); for(自动it=range.first;it!=range.second;++it){ auto&entry=it->second;//it->first将是键/位置 std::cout

如何在c+;中使用哈希在同一个键下存储多个值+; 我想用Hash技术在C++中建立一个电话簿项目。我需要做的是它必须根据位置搜索联系人。所以我将把位置作为键值,但是在同一个位置会有多个联系人。因此,如何在同一个键(位置)上存储多个值(姓名,电话号码)。。它是一个允许使用相同密钥的多个条目的哈希映射。要为每个条目存储多个属性,可以使用结构或类。最后,它可能是这样的: struct contact_t { std::string name; std::string phone_number; } std::unordered_multimap<std::string, contact_t> directory; int main(int argc, char *argv[]) { // Add entry contact_t contact; contact.name = "Fooman"; contact.phone_number = "00 000 00000000"; directory.emplace("Barcity", contact); // List all entries of one city auto range = directory.equal_range("Barcity"); for (auto it = range.first; it != range.second; ++it) { auto &entry = it->second; // it->first would be the key/location std::cout << "Name: " << entry.name << "Phone: " << entry.phone_number << std::endl; } } struct contact\t{ std::字符串名; std::字符串电话号码; } std::无序的_多映射目录; int main(int argc,char*argv[]) { //添加条目 联系方式; contact.name=“Fooman”; contact.phone_number=“00 000 00000000”; 安置目录(“酒吧城”,联系人); //列出一个城市的所有条目 自动范围=目录。相等范围(“Barcity”); for(自动it=range.first;it!=range.second;++it){ auto&entry=it->second;//it->first将是键/位置 std::cout,c++,hash,C++,Hash,如果您使用的是C++11或更新版本,则可以使用std::unordered_multimap来存储每个键的多个条目(例如,每个位置有多个条目)。它是一个哈希映射,允许使用同一个键的多个条目。要存储每个条目的多个属性,可以使用一个结构或类。最后,它可以如下所示: struct contact_t { std::string name; std::string phone_number; } std::unordered_multimap<std::string, conta

如果您使用的是C++11或更新版本,则可以使用
std::unordered_multimap
来存储每个键的多个条目(例如,每个位置有多个条目)。它是一个哈希映射,允许使用同一个键的多个条目。要存储每个条目的多个属性,可以使用一个结构或类。最后,它可以如下所示:

struct contact_t {
    std::string name;
    std::string phone_number;
}

std::unordered_multimap<std::string, contact_t> directory;

int main(int argc, char *argv[])
{
    // Add entry
    contact_t contact;
    contact.name = "Fooman";
    contact.phone_number = "00 000 00000000";
    directory.emplace("Barcity", contact);

    // List all entries of one city
    auto range = directory.equal_range("Barcity");
    for (auto it = range.first; it != range.second; ++it) {
        auto &entry = it->second; // it->first would be the key/location
        std::cout << "Name: " << entry.name
                  << "Phone: " << entry.phone_number << std::endl;
    }
}
struct contact\t{
std::字符串名;
std::字符串电话号码;
}
std::无序的_多映射目录;
int main(int argc,char*argv[])
{
//添加条目
联系方式;
contact.name=“Fooman”;
contact.phone_number=“00 000 00000000”;
安置目录(“酒吧城”,联系人);
//列出一个城市的所有条目
自动范围=目录。相等范围(“Barcity”);
for(自动it=range.first;it!=range.second;++it){
auto&entry=it->second;//it->first将是键/位置

std::cout Use
std::unordered_multimap
@DannyuNDos这是散列技术的一部分吗?这是一个能够用同一个键存储多个元素的散列表。谢谢,我会检查一下……“但是在同一个位置会有多个联系人。那么如何在同一个键(位置)下存储多个值(姓名、电话号码)。”-您在这里谈论的是两件不同的事情。您希望由于多个条目或由于不同的属性(姓名、电话号码等)而获得多个值吗使用
std::unordered_multimap
@DannyuNDos这是散列技术的一部分吗?这是一个能够用同一个键存储多个元素的散列表。谢谢,我会检查一下……“但是在同一个位置会有多个联系人。那么如何在同一个键(位置)下存储多个值(姓名、电话号码)。”-您在这里谈论的是两件不同的事情。您希望由于多个条目或由于一个条目的不同属性(姓名、电话号码等)而获得多个值吗?