【问题标题】:get value from hash table从哈希表中获取值
【发布时间】:2015-12-23 18:42:01
【问题描述】:
Ticket* Championship::findTicketByFanID(unsigned int id) {

    hTabTickets::iterator it = tickets.begin();

    while(it != tickets.end()) {
        if(it->getOwnerID() == id)
            return it;
    }

}

你好

我想返回 iterator 上的对象。 tickets 是一个哈希表,其中包含 Ticket 类型的对象。当我搜索它时,有什么解决方案可以返回该对象?

我希望能够做到这一点,所以我可以做到Ticket t1 = findTicketByFan(id);

hTabTickets:

struct eqTicket {
    bool operator() (const Ticket &b1, const Ticket &b2) const{
        return b1.getID() == b2.getID();
    }
};

struct hTicket{
    int operator() (const Ticket &b1) const{
        return b1.getID();
    }


};

typedef tr1::unordered_set<Bilhete, hTicket, eqTicket> hTabTickets;

问候

【问题讨论】:

  • return it-&gt;second;
  • 而且您没有涵盖所有控制路径。请显示hTabTickets 的确切类型。

标签: c++ hashtable


【解决方案1】:

也许你想要这个:

Ticket* Championship::findTicketByFanID(unsigned int id) {
  for (hTabTickets::iterator it = tickets.begin(); it != tickets.end(); ++it) {
      if(it->getOwnerID() == id)
          return &(*it);
  }
  return NULL;
}

如果您非常频繁地搜索 id,您可能需要将 set 更改为 map

【讨论】:

  • 从 'const Ticket*' 到 'Ticket*' 的无效转换 [-fpermissive]
  • 如果ticketsfindTicketByFanIDconst,你不能(或不应该)返回一个非常量指针。将函数的返回类型更改为const Ticket*
【解决方案2】:

引用在这里非常有用。通常返回本地指针不是一个好主意。下面给出了您的案例的示例。您的代码有两个问题:(a)并非所有路径都返回值和(b)哈希表应该快速定位(理想情况下为 O(1)),因此循环是一个坏主意。此外,it 不会在循环中递增。

class Campeonato {
    // Using STL hashmap
    map<unsigned int, int> tickets;

    public:       
    // the return value tells you whether to use iterator or not.
    bool findTicketByFanID(unsigned int id, map<unsigned int, int>::iterator &it) {
        // std::map provides method find
        it = tickets.find(id);
        if (it != tickets.end())
            return true;

        return false;
    }
};    

【讨论】:

    【解决方案3】:

    我希望能够做到这一点,所以我可以做到 Ticket t1 = findTicketByFan(id);

    如果这是您的意图,返回类型不正确,它应该按值或 (const) 引用返回,而不是指针。另一个问题,您使用std::unordered::set 并尝试循环搜索,但您应该使用std::unordered_set::find() 代替:

    Ticket Championship::findTicketByFanID(unsigned int id) 
    {
        hTabTickets::iterator it = tickets.find( Ticket( id ) );
        if( it == tickets.end() ) // not found do something
            throw std::runtime_error( "Ticket not found" );
        return *it;
    }
    

    如果创建临时票太贵,你应该改用std::unordered_map&lt;int,Ticket&gt; 并使用 id 作为键。那么这个函数就是:

    Ticket Championship::findTicketByFanID(unsigned int id) 
    {
        hTabTickets::iterator it = tickets.find( id );
        if( it == tickets.end() ) // not found do something
            throw std::runtime_error( "Ticket not found" );
        return it->second;
    }
    

    【讨论】:

      【解决方案4】:
      hTabTickets::iterator it = tickets.begin()
      

      'it'变量的类型既不是Ticket,也不是Ticket*,它是一个包含键值变量的对,你用错了,可能你需要写:

      it->second    
      

      访问票证本身

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-11
        • 2016-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-10-31
        • 2017-05-26
        • 1970-01-01
        • 2017-02-16
        相关资源
        最近更新 更多