简体   繁体   English

迭代C ++映射键

[英]iterating c++ map keys

I'm trying to iterate a C++ std::map s' keys and push them onto an integer vector, so later I can get the minimum key/object. 我正在尝试迭代C ++ std::map s的键并将它们推到整数向量上,因此以后我可以获得最小的键/对象。

Here is the relevant snippet: 以下是相关代码段:

//shortest path algorithm
void Graph::shortest_path(std::vector<std::vector<int>> &_matrix) {
  queue<int> unvisited;
  queue<int> visited;

  //initialize unvisited
  for (int u=0; u<_matrix.size(); u++)
    unvisited.push(u);

  vector<Node>  _neighbors;
  Node neighbor;
  Node * current=NULL;
  Node * previous=NULL;

  while (unvisited.size()>0) {
    int r = unvisited.front();
    if (current==NULL) {
      current = new Node(r+1);
    } else {
      previous = current;
      current = new Node(r+1);
      current->setPrev(*previous);
    }

    cout << "current:" << current->getLabel();
    cout << " _neighbors=" << endl;
    _neighbors = neighbors(_matrix,r+1);

    for (int n=0; n<_neighbors.size(); n++) {
      cout << _neighbors[n].getLabel() << ",";
      current->addToAdjacenyList(_neighbors[n]);
    }

    Node * backToRoot = NULL;
    std::map<int,Node*> minMap;
    for(int b = 0; b<current->getAdjacenyList().size(); b++) {
      backToRoot = &current->getAdjacenyList()[b];
      int distance = backToRoot->getWeight();

      while (backToRoot!=NULL) {
        backToRoot = backToRoot->getPrev();
        distance = distance + backToRoot->getDistance();
      }

      backToRoot->setDistance(distance);
      //add to map or priority queue were the key is distance & value is the node
      minMap[distance] = backToRoot;
      //get the keys from the map & get the minimum key/distance
      vector<int> keys;
      //LOOK BELOW
      for(std::map<int,Node*>::iterator it = minMap.begin(); it !=  minMap.end(); ++it) {
        keys.push_back(it->first);
        cout << it->first << "\n";
      }
      int min_key = std::min_element(keys.begin(),keys.end());
      cout << "min_key:" << min_key ;
    }

    cout << endl;
    unvisited.pop();
    visited.push(r);
  }//end while unvisited is NOT empty
}//end shortest path

However I getting this error: 但是我得到这个错误:

Graph.cpp:136:71: error: cannot convert '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' to 'int' in initialization
int min_key = std::min_element(keys.begin(),keys.end());

Look in the code snippet for LOOK BELOW comment for exact area of the problem. 在代码段中查找下面的注释,以查找问题的确切区域。

How can I fix the syntax? 如何修复语法?

The keys in std::map are sorted according to the < operator. std::map中的键是根据<运算符排序的。 You can get the minimum key with 您可以通过获得最小密钥

minMap.begin()->first

As for your error, std::min_element returns an iterator not an int. 至于您的错误, std::min_element返回一个迭代器而不是一个int。 You need to deference the iterator first. 您需要先引用迭代器。

// Wrong
int min_key = std::min_element(keys.begin(),keys.end());

// Correct
auto it = std::min_element(keys.begin(),keys.end());
int min_key = *it;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM