title: C++ set how to sort a set by value and search by key
body: <p>I am implementing <code>A*</code> shortest path algorithm. The Openlist part stores all the nodes that will be visited. The list is like a priority queue that the first element has the minimum cost value and so in each iteration, we just pop up the first element and visit it. But inside the iteration, we need to loop over the neighbors of this node and check if the neighbor is in Openlist. </p>
  
  <p>So that means this Openlist needs to support two types of operations: </p>
  
  <ol>
  <li>automatic sorting  </li>
  <li>look up a node(by its ID)</li>
  </ol>
  
  <p>The problem here is that Openlist will sort by the cost value, while look up need to be based on the ID of the neighbor node(the adjacent nodes). So I was considering using set, where the elements are the nodes. But I don't know how to look up an element by its ID in this set.</p>
  
  <pre><code>struct astar_node
  {
      string id;
      double f;  //estimated cost;
      double g;  //distance from source to this node;
      double h;  //heuristic cost from this node to target;
  };  
  
  struct openlist_compare
  {
      bool operator()(const astar_node &amp;node1, const astar_node &amp;node2 ){
             return node1.f &lt; node2.f ;
      }
  };
  
  std::set&lt;astar_node, openlist_compare&gt; Openlist;
  </code></pre>