【问题标题】:searching in map through find function for key value is not happening properly通过查找功能在地图中搜索键值未正确发生
【发布时间】:2016-05-05 11:35:02
【问题描述】:

在下面的代码中,我通过键值在 map 中执行搜索,但无法获得所需的结果,尽管两个列表中都存在公共元素,并且 find with map 应该给出预期的输出。 使用地图执行查找时是否有任何问题?这是正确的方法吗?

输入

list1:10->15->4->20 list2:10->2->4->8

预期输出: 常见->数据=4 common->data=10

 #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <map>
    using namespace std;
    struct node
    {
        int data;
        struct node* next;
    };
    /* A utility function to insert a node at the beginning of 
       a linked list*/
    void push(struct node** head_ref, int new_data);
    /* A utility function to insert a node at the begining of a linked list*/
    void push (struct node** head_ref, int new_data)
    {
        /* allocate node */
        struct node* new_node =
            (struct node*) malloc(sizeof(struct node));
        /* put in the data */
        new_node->data = new_data;
        /* link the old list off the new node */
        new_node->next = (*head_ref);
        /* move the head to point to the new node */
        (*head_ref) = new_node;
    }
     /*insert the head1 into map and find the head2 in map*/
    int create_hash(struct node* head1,struct node* head2)
    {
      int flag=0;
     map<node*,bool> intersect;

    while(head1!=NULL)
    {
    printf("first_list->data=%d\n",head1->data);
    intersect[head1]=true;
    head1=head1->next;
    }
    while(head2!=NULL)
    {
    printf("second_list->data=%d\n",head2->data);
    if (intersect.find(head2)!= intersect.end())
           printf("common->data=%d\n",head2->data); 
           flag=1;
           head2=head2->next;
    }
    if(flag==1)
    {
    return 0;
    }
    return -1;
    }
    /* Drier program to test above function*/
    int main()
    {
        /* Start with the empty list */
        struct node* head1 = NULL;
        struct node* head2 = NULL;
        int ret_val;
        struct node* unin = NULL;
        /*create a linked lits 10->15->4->20 */
        push (&head1, 20);
        push (&head1, 4);
        push (&head1, 15);
        push (&head1, 10);
        /*create a linked list 10->2->4->8 */
        push (&head2, 10);
        push (&head2, 2);
        push (&head2, 4);
        push (&head2, 8);
        ret_val = create_hash (head1, head2);
        return 0;
    }

【问题讨论】:

  • 这是 C 代码和小 C++ 的混合体。这是主要问题

标签: c++ stl linked-list


【解决方案1】:

您使用第一个列表中的指向节点的指针填充intersect 映射,然后在该映射中搜索指向第二个列表中节点的指针。由于列表之间不共享节点,因此搜索永远不会成功。

在这个实现的上下文中,映射应该包含数据值,而不是节点指针。

【讨论】:

  • 感谢您的回复。是否可以通过修改当前共享代码来执行基于节点的搜索。
  • 地图 相交;将解决我的问题,但如何通过 map 解决同样的问题?
  • 您可以使用std::find_if(intersect.begin(), intersect.end(), [head2](node* n){return n-&gt;data == head2-&gt;data;}) 而不是intersect.find(head2)。 (如果您不能使用 C++11 lambda,则有等效但更冗长的构造)。
  • 我正在尝试使用 -std=c++0x 选项进行相同的编译,但仍然出现编译错误。
猜你喜欢
  • 2011-12-07
  • 2012-05-21
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多