Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 图C++中的地图的有效循环_Loops_C++11 - Fatal编程技术网

Loops 图C++中的地图的有效循环

Loops 图C++中的地图的有效循环,loops,c++11,Loops,C++11,我试图在地图数据结构上找到一个有效的循环 映射结构映射以下整数: 1 2, 2 3, 3 1, 4 1, 4 5, 5 3, 5 6, 5 7, 5 8, 6 4, 7 6, 8 9, 9 10 生成的映射如下所示: 1| 2 2| 3 3| 1 4| 1 5 5| 3 6 7 8 6| 4 7| 6 8| 9 9| 10 Start : 4 Result : 1(1) 2(2) 5(1) 3(2) 6(2) 7(2) 8(2

我试图在地图数据结构上找到一个有效的循环

映射结构映射以下整数:

1 2, 2 3, 3 1, 4 1, 4 5, 5 3, 5 6, 5 7, 5 8, 6 4, 7 6, 8 9, 9 10
生成的映射如下所示:

1| 2   
2| 3   
3| 1   
4| 1   5   
5| 3   6   7   8   
6| 4   
7| 6   
8| 9   
9| 10

Start : 4
Result : 
1(1) 2(2) 5(1) 3(2) 6(2) 7(2) 8(2)
有谁能建议如何高效地循环可能的递归方法,以便在给定(比如)4开头的情况下,结果是

1(1), 2(2), 5(1), 3(2), 6(2), 7(2), 8(2), 9(3), 10(4)
因此,我们的想法是使用每个内键作为外键,从给定的外键开始。例如,对于外部4,内部键为5和1。因此,使用5和1作为外键以获得内键3、6、7、8和2,该过程应继续将内键映射到外键。每次跳跃应保持跑步总数。因此,它可能会解决递归问题,而不是循环

如果您达到了起点(在上述场景中为4),或者没有更多的内部键(例如,10没有映射),则该过程应该停止

从第44行开始的循环仅执行上述两个级别,这是不够的

#include <iostream>
#include <map>
#include <sstream>

int digit_count(int number) {
  int digits = 0;
  if (number < 0) digits = 1; // remove this line if '-' counts as a digit
  while (number) {
      number /= 10;
      digits++;
  }
  return digits;
}

int main() {

  int v1, v2;
  std::map< int, std::map< int, int> > m;
  std::istringstream  stm {"1 2  2 3  3 1  4 1  4 5  5 3  5 6  5 7  5 8  6 4  7 6  8 9  9 10"};

  while (stm >> v1 >> v2) {
    m[v1];
    m[v1][v2] = 1;
  }

  std::cout << "Map layout " << "\n";
  std::string ss = "";
  int dc = digit_count(m.rbegin()->first); // equals max number

  for (const auto & p : m) {
    std::cout << p.first << ss.append(" ",  (dc - digit_count(p.first))) << "| ";
    for (const auto & val : p.second)
      std::cout << val.first << "   ";
    ss = "";
    std::cout << "\n";
  }

  int start {4};

  std::cout << "\nStart : " << start << "\n";
  std::cout << "Result : " << "\n";

  // efficient loop
  for (const auto & e : m[start]) {
    std::cout << e.first << "(" << e.second << ") ";
    for (const auto & x : m[e.first])
      std::cout << x.first << "(" << (e.second + x.second) << ") ";
  }
  std::cout << "\n";
  return 0;
}

任何帮助都将不胜感激。

花了我一段时间,但我已经回答了自己的问题,并认为我应该更新帖子。我能想到的找到解决方案的唯一方法是创建一个递归函数。我认为不可能用循环来实现。我没有选择Dijkstra的算法,因为没有权重要考虑,并且这个递归函数的结果用作红黑树的输入,其中红黑树的每个节点都保持哈希表无序的映射。 因此,对组合红黑树/哈希表的查询结果是logn,以查找最短路径。问题在于,正如您从下面看到的,提供输入是递归的,效率低下

#include <iostream>
#include <map>
#include <sstream>
#include <set>
#include <vector>
#include <fstream>

int digit_count(int number) {
  int digits = 0;
  if (number < 0) digits = 1; // remove this line if '-' counts as a digit
  while (number) {
      number /= 10;
      digits++;
  }
  return digits;
}

struct vertex {
  int point;
  mutable bool visited{false};
  int id; 
};

void clear_visited(std::map<int, vertex>& verteces) {
  for (const auto & e : verteces) {
    e.second.visited = false;
  }
}

void traverse_graph(std::map<int, vertex>& verteces, const vertex & v, std::map< vertex, std::map< vertex, int> >& graph, int& counter) {
  if (verteces[v.id].visited)
    return;

  ++counter;
  verteces[v.id].visited = true;

  std::cout << v.point << "(" << counter << ") ";
  for (const auto & e : graph[v]) {
    traverse_graph(verteces, e.first, graph, counter);
  }  
}

void start_traverse_graph(std::map<int, vertex>& verteces, const vertex & v, std::map< vertex, std::map< vertex, int> >& graph) {
  if (verteces[v.id].visited)
    return;

  verteces[v.id].visited = true;

  for (const auto & e : graph[v]) {
    int counter{0};
    clear_visited(verteces);
    verteces[v.id].visited = true;
    traverse_graph(verteces, e.first, graph, counter);
  } 
}

bool operator<(vertex a, vertex b) { return a.point < b.point; }

int main (int argc, char *argv[]) {

  vertex v1, v2;
  std::map< vertex, std::map< vertex, int> > m;
  std::istringstream  stm {"1 2  2 3  3 1  4 1  4 5  5 3  5 6  5 7  5 8  6 4  7 6  8 9  9 10"};
  std::set<vertex> vertecesSet;
  std::map<int, vertex> verteces;

  while (stm >> v1.point >> v2.point) {
    v1.id = v1.point;
    v2.id = v2.point;
    m[v1];
    m[v1][v2] = 1;
    vertecesSet.insert({v1.point, false, v1.point});
    vertecesSet.insert({v2.point, false, v2.point});
  }

  for(auto & el : vertecesSet)
    verteces[el.id] = std::move(el); // dont need set objects anymore so move them

  std::cout << "Map layout " << "\n";
  std::string ss = "";
  int dc = digit_count(m.rbegin()->first.point); // equals max number

  for (const auto & p : m) {
    std::cout << p.first.point << ss.append(" ",  (dc - digit_count(p.first.point))) << "| ";
    for (const auto & val : p.second)
      std::cout << val.first.point << "  ";
    ss = "";
    std::cout << "\n";
  }

  vertex start {5,false,5};

  std::cout << "\nStart : " << start.point << "\n";

  start_traverse_graph( verteces, start, m);
  std::cout << "\n";
  return 0;
}

解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包含一个重现您的问题的示例,以及您在调试器中所做的观察。也许我完全错误地理解了您的问题,但我看不出std::map>如何保存所描述的数据。对我来说,std::map>似乎是一个更好的选择。您的输入数据是一个图形的表示,您的问题归结为为为该图形找到一个表达式,例如。