关于vector和hash_map查找,遍历效率的测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
散列表是一种常见的数据结构,可以用来实现哈希表,而哈希表又可以用来实现键值对的存储和查找。在实现QQ的账号申请和登录功能时,我们可以使用散列表来存储用户的账号和密码信息,具体步骤如下: 1. 定义用户账号和密码的数据结构 我们可以定义一个结构体来存储用户的账号和密码信息,例如: ```c++ struct User { std::string account; // 用户账号 std::string password; // 用户密码 }; ``` 2. 定义散列表的数据结构 散列表由若干个桶组成,每个桶里存储着一组键值对。我们可以使用vector来表示每个桶,使用unordered_map来表示整个散列表,例如: ```c++ using Bucket = std::vector<User>; // 每个桶是一个vector using HashTable = std::unordered_map<int, Bucket>; // 整个散列表是一个unordered_map ``` 其中,键值对的键是一个整数(哈希值),值是一个桶(vector)。 3. 实现散列函数 散列函数可以将任意长度的输入(例如用户的账号)映射到固定长度的输出(例如一个整数),并且满足以下两个条件: - 相同的输入映射到相同的输出; - 不同的输入尽可能映射到不同的输出。 我们可以采用简单的字符串哈希函数来实现散列函数,例如将每个字符的ASCII码相加后取余数: ```c++ int hash_function(const std::string& str, int bucket_size) { int hash_value = 0; for (char c : str) { hash_value += c; } return hash_value % bucket_size; } ``` 其中,`bucket_size`表示散列表的大小(即桶的数量)。 4. 实现账号申请功能 当用户填写完账号和密码后,我们需要将其存储到散列表中。具体步骤如下: - 计算账号的散列值; - 在散列表中查找对应的桶; - 将新用户添加到桶中。 代码如下: ```c++ void register_account(const std::string& account, const std::string& password, HashTable& hash_table) { int hash_value = hash_function(account, hash_table.size()); Bucket& bucket = hash_table[hash_value]; for (const User& user : bucket) { if (user.account == account) { std::cout << "Account already exists!" << std::endl; return; } } User new_user = {account, password}; bucket.push_back(new_user); std::cout << "Account registered successfully!" << std::endl; } ``` 5. 实现登录功能 当用户输入账号和密码后,我们需要在散列表中查找对应的账号和密码是否匹配。具体步骤如下: - 计算账号的散列值; - 在散列表中查找对应的桶; - 遍历桶中的用户,查找账号和密码是否匹配。 代码如下: ```c++ bool login(const std::string& account, const std::string& password, const HashTable& hash_table) { int hash_value = hash_function(account, hash_table.size()); const Bucket& bucket = hash_table[hash_value]; for (const User& user : bucket) { if (user.account == account && user.password == password) { std::cout << "Login successful!" << std::endl; return true; } } std::cout << "Account or password is incorrect!" << std::endl; return false; } ``` 完整代码示例: ```c++ #include <iostream> #include <string> #include <vector> #include <unordered_map> struct User { std::string account; // 用户账号 std::string password; // 用户密码 }; using Bucket = std::vector<User>; // 每个桶是一个vector using HashTable = std::unordered_map<int, Bucket>; // 整个散列表是一个unordered_map int hash_function(const std::string& str, int bucket_size) { int hash_value = 0; for (char c : str) { hash_value += c; } return hash_value % bucket_size; } void register_account(const std::string& account, const std::string& password, HashTable& hash_table) { int hash_value = hash_function(account, hash_table.size()); Bucket& bucket = hash_table[hash_value]; for (const User& user : bucket) { if (user.account == account) { std::cout << "Account already exists!" << std::endl; return; } } User new_user = {account, password}; bucket.push_back(new_user); std::cout << "Account registered successfully!" << std::endl; } bool login(const std::string& account, const std::string& password, const HashTable& hash_table) { int hash_value = hash_function(account, hash_table.size()); const Bucket& bucket = hash_table[hash_value]; for (const User& user : bucket) { if (user.account == account && user.password == password) { std::cout << "Login successful!" << std::endl; return true; } } std::cout << "Account or password is incorrect!" << std::endl; return false; } int main() { HashTable hash_table(100); register_account("alice", "123456", hash_table); register_account("bob", "abcdef", hash_table); login("alice", "123456", hash_table); login("alice", "654321", hash_table); login("charlie", "abcdef", hash_table); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值