哪种方法从这两种使用哈希映射的方法中更好?

huangapple go评论70阅读模式
英文:

Which approach is better from these two using hashmaps..?

问题

Approach 2 is better practice.

方法2是更好的实践。

英文:

Approach 1: This method uses an integer account_num to look up a user in a hashmap. It then iterates over the list of users associated with that account number to verify the entered password. It returns the address of the user object if login is successful. The major problem is that it attempts to return the address of a local variable, which can lead to undefined behavior. Moreover, there's no clear feedback if the account_num doesn't exist in the hashmap.

User* login() 
{   
    bool correct_id= false;
    User* user= nullptr;
    while (correct_id == false)
    {
        cout<<"Enter email"<<endl;
        int account_num;
        cin>> account_num;
        cout<<"Enter password"<<endl;
        string password;
        cin>> password;
        if (this->hashmap.find(account_num)!=this->hashmap.end())
        {
            for(User user_node: hashmap[account_num])
            {
                if(account_num==user_node.account_num && password==user_node.password)
                {
                    cout<<"Login successful, loading menu";
                    user= &user_node;
                    correct_id = true;
                }
            }
            return user;
        }
    }
}

Approach 2: This method uses the user's email as a key to access the hashmap directly. It then checks if the entered password matches the stored one for that email. If the login is successful, it returns the address of the user object in the hashmap. It provides clear feedback if the email doesn't exist or the password is incorrect.

User* Login()
{
    bool correct_id = false;
    User* user = nullptr;
    while(!correct_id)
    {
        string email,password;
        cout<<"Enter Your Email"<<endl;
        cin>>email;
        cout<<"Enter Your Password"<<endl;
        cin>>password;
        if(this->hashmap.find(email)!=this->hashmap.end())
        {
            User* user_ptr= this->hashmap[email];
            if(user_ptr->password==password)
            {
                cout<<"Login Successful,Loading Menu";
                user = user_ptr;
                correct_id = true;
            }
            else
            {
                cout<<"incorrect password. Try Again";
            }
        }
        else
        {
            cout<<"Email Not Found. Try Again";
        }
    }
    return user;
}

I just want to know which approach is better practice..

答案1

得分: 0

第二种C++用户登录功能的方法使用用户的电子邮件作为唯一标识符。它通过使用电子邮件作为键在无序映射(哈希映射)中存储的用户数据,提供对用户数据的直接访问。由于哈希映射的访问时间复杂度为O(1),因此这种方法非常高效。

在输入不正确的电子邮件或密码时,系统会通知用户并要求重试,从而提供更好的用户体验。登录成功后,它会返回一个指向用户对象的指针,确保通过引用哈希映射中的现有对象而不是临时或本地对象来进行正确的内存管理。

虽然这种方法以明文形式处理密码,这并不安全,但它提供了通过对密码进行哈希处理来增强安全性的明确途径。总体而言,与第一种方法相比,这种方法更加传统、高效和用户友好。它与用户身份验证系统的常见行业实践紧密配合,避免了潜在的内存管理问题。

英文:

The second approach for a user login function in C++ uses a user's email as a unique identifier. It provides direct access to user data stored in an unordered map (hashmap) using the email as a key. This approach is efficient due to the O(1) access time complexity of hashmaps.

Upon incorrect email or password entry, the user is informed and asked to try again, providing a better user experience. When login is successful, it returns a pointer to the user object, ensuring correct memory management by referencing an existing object in the hashmap, not a temporary or local one.

While this approach handles passwords in plain text, which is not secure, it provides a clear pathway for security enhancement by hashing passwords. Overall, it's a more conventional, efficient, and user-friendly approach compared to the first one. It aligns closely with common industry practices for user authentication systems and avoids potential memory management issues.

答案2

得分: 0

好的,以下是翻译好的部分:

  1. Well, I specialize in Javascript, but I'll take a crack at it.
    嗯,我专注于JavaScript,但我会试着解答。

  2. The answer is it depends on the details of the program, I assume this is from a homework exercise. You should have posted the hashmap definition for better understanding.
    答案取决于程序的细节,我猜这是一个作业练习。你应该发布哈希映射的定义以便更好地理解。

  3. In terms of memory and accessibility and syntax, the first one is better. I've worked in banking and can't recall any system that uses email addresses for map keys.
    就内存、可访问性和语法而言,第一个更好。我在银行业工作过,但不记得有任何系统使用电子邮件地址作为映射键。

  4. Both functions feature 2 searches (if or in the first one, for) which lead me to believe this is a hashmap that contains another data structure.
    这两个函数都包含2次搜索(第一个使用if,第二个使用for),这让我认为这是一个包含另一个数据结构的哈希映射。

  5. In that case, the second one is wrong. The syntax doesn't work. You're trying to first access the index and then access what is inside it. But whatever is inside that index is not this->hashmap[email]. By writing User *user_ptr = this->hashmap[email] you're making a pointer to the index itself, not whatever data struct is inside it.
    在这种情况下,第二个是错误的。语法不正确。你试图首先访问索引,然后访问其中的内容。但是,索引内部的内容不是this->hashmap[email]。通过编写User *user_ptr = this->hashmap[email],你正在创建一个指向索引本身的指针,而不是其中的数据结构。

  6. Not to repeat myself, but your answer is basically unanswerable without the hashmap definition.
    不想重复,但是如果没有哈希映射的定义,你的问题基本上无法回答。

英文:

Well, I specialize in Javascript, but I'll take a crack at it.
The answer is it depends on the details of the program, I assume this is from a homework exercise. You should have posted the hashmap definition for better understanding.

In terms of memory and accessibility and syntax, the first one is better. I've worked in banking and can't recall any system that uses email addresses for map keys.

Both functions feature 2 searches (if or in the first one, for) which lead me to believe this is a hashmap that contains another data structure.
In that case, the second one is wrong. The syntax doesn't work. You're trying to first access the index and then access what is inside it. But whatever is inside that index is not this->hashmap[email]. By writing User *user_ptr = this->hashmap[email] you're making a pointer to the index itself, not whatever data struct is inside it.
Not to repeat myself, but your answer is basically unanswerable without the hashmap definition.

答案3

得分: 0

哈希映射的类型是什么,是无序映射还是自定义结构体?
两者都有点业余,但我假设你是初学者。

从第一个中,我可以推断这是某种嵌套的哈希映射,不是典型的键值哈希映射。
如果是这种情况,第一个更好。

第一个更好,因为第二个存在明显的语法问题。hashmap.find(email)搜索索引没问题,但在下一行,你将指针再次分配给了索引,这是错误的。你应该将指针分配给索引内部的任何内容(用户?),而不是索引本身。

这些代码差异如此之大,以至于我甚至无法确定它们是否使用相同的哈希映射。它们是吗?如果是的话,那么第二个是错误的。

英文:

What is the type of hashmap, is it an unordered map or a custom struct?
Both are amateurish, but I assume you're a beginner.

From the first one, I can infer that this is some type of nested hashmap, it's not the typical key, value hashmap.
If that is the case, the first one is better.

The first one is better because the second one has a glaring syntax issue. hashmap.find(email) searches the index alright, but in the line after, you are assigning the pointer to the index again, which is wrong. You should assign the pointer to whatever(USER?) is inside the index, not the index itself.

These codes are so different I can't even tell if they are using the same hashmap. Are they? If they are, then the second one is wrong.

答案4

得分: 0

第一段代码比第二段代码更加初级,尽管第二段代码包含的错误比第一段更多。

关于第一段代码返回本地变量地址的评论是错误的。它并不返回本地变量的地址,而是返回位于函数外部存储的哈希映射变量的地址。

user_node 创建了一个用户对象的副本,然后将地址存储到 user_node,然后将其分配给指针 user。因此,指针 user 指向登录函数之外的对象。

尽管 user_node 变量不是本地的,但用户指针及其所指向的地址超出了函数范围。因此,它返回哈希映射中的有效对象地址,而不是像您所说的本地变量地址。

第二段代码存在语法问题,哈希映射的搜索没有意义。为什么要返回哈希映射索引的指针呢?您需要返回用户对象的指针,而不是索引。通过电子邮件地址搜索哈希映射很笨拙,更不用说相关的安全问题了。

总的来说,第一段代码值得得到 B- 的评价,而第二段代码只能得到 F。

英文:

Both pieces of code are sophomoric although the second contains more errors than the first.

Your comment on the first code returning the address of the local variable is wrong. It does not return the address of the local variable, it returns the address of the hashmap variable, which is stored outside the function.

user_node makes a copy of the User object and then stores the address to user_node, which is then assigned to the pointer user. So pointer user points to an object beyond the login function.

The user pointer and the address it points to extend beyond the function scope even though the user_node variable doesn't. So it returns an address to a valid object in the hashmap, not an address to a local variable as you said.

The second code has syntax issues, the hashmap search doesn't make sense. Why return a pointer to the hashmap index? You need to return a pointer to the User object not the index. Searching a hashmap by email address is awkward, not to mention the accompanying security issues.

Overall, the first code deserves a B- the second one deserves a F.

答案5

得分: 0

第二种方法在我看来更专业且节省时间:
一些原因如下:

  1. 错误处理:代码通过检查电子邮件是否存在以及通知用户密码不正确或未找到电子邮件来处理不同的错误情况。
  2. 用户体验:它通过显示清晰的消息并允许用户更正其输入提供了用户友好的体验。
  3. 鲁棒性:代码使用循环重复提示用户,直到输入正确的电子邮件和密码,防止意外登录或未经授权的访问。
  4. 安全性(基本):尽管缺乏高级安全措施,但代码通过将用户输入密码与存储的密码进行比较,演示了一种简单的基于密码的身份验证机制。
英文:

Second approach looks more professional and time saving to me:
few reasons why:
1.Error handling: The code handles different error scenarios by checking if the email exists and informing the user about incorrect passwords or emails not found.
2.User experience: It provides a user-friendly experience by displaying clear messages and allowing users to correct their input.
3.Robustness: The code uses a loop to repeatedly prompt the user until the correct email and password are entered, preventing accidental logins or unauthorized access.
4.Security (basic): Although it lacks advanced security measures, the code demonstrates a simple password-based authentication mechanism by comparing user input passwords with stored passwords.

huangapple
  • 本文由 发表于 2023年6月19日 07:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定