简体   繁体   English

使用STL映射时,模板参数数量错误?

[英]Using an STL map, wrong number of template arguments?

I'm having a little trouble utilizing a map, and I have never really used them before so I am really struggling here 我在使用地图时遇到了一些麻烦,而且我之前从未真正使用过它们,所以我在这里很挣扎

My Code is as follows: 我的代码如下:

MSTapp.h MSTapp.h

 5 #ifndef MSTAPP_H
 6 #define MSTAPP_H
 7
 8 #include"Graph.h"
 9
 10 #include<iostream>
 11 #include<map>
 12 #include<vector>
 13 #include<string>
 14
 15 using namespace std;
 16
 17 class MSTapp
 18 {
 19  public:
 20   void read_graph();
 21   void print_v();
 22 //  void print_e();
 23
 24  private:
 25    Graph my_graph;
 26 };
 27
 28 #endif
 29

MSTapp.cpp MSTapp.cpp

 5 #include"MSTapp.h"
 6 #include"Graph.h"
 7
 8 #include<iostream>
 9 #include<map>
 10 #include<vector>
 11 #include<stdlib.h>
 12 #include<sstream>
 13 #include<string>
 14
 15 using namespace std;
 16
 17 void MSTapp::read_graph()
 18 {
 19  string s;
 20  int count = 0;
 21
 22  while(getline(cin, s))
 23  {
 24   if(count == 0)
 25   {
 26    istringstream my_words(s);
 27    string word;
 28    while(my_words >> word)
 29    {
 30     my_graph.add_vertex(word);
 31    }
 32   }
 33  else
 34   {
 35    string first;
 36    string last;
 37    int key;
 38
 39    first = s.substr(0, s.find(" "));
 40    s.erase(0,s.find(" ")+1);
 41    last = s.substr(0, s.find(" "));
 42    s.erase(0,s.find(" ")+1);
 43    key = atoi(s.c_str());
 44
 45    my_graph.add_edge(first, last, key);
 46   }
 47  }
 48 }
 49
 50 void MSTapp::print_v()
 51 {
 52  my_graph.print_vertices();
 53 }

Graph.h Graph.h

 5 #include"MSTapp.h"
 6 #include<map>
 7 #include<iostream>
 8 #include<string>
 9 #include<vector>
 10 #include<list>
 11
 12 using namespace std;
 13
 14 #ifndef GRAPH_H
 15 #define GRAPH_H
 16
 17 class Graph
 18 {
 19  public:
 20 //  Graph();
 21 //  ~Graph();
 22   void add_vertex(string name);
 23   void print_vertices();
 24   void add_edge(string from, string to, int weight);
 25  // void print_edges();
 26 //  void min_span_tree(string start);
 27
 28  private:
 29  // MinPriority min_queue;
 30
 31   class Vertex
 32   {
 33    public:
 34     string name;
 35     string pi;
 36     int key;
 37   };
 38   class Neighbor
 39   {
 40    public:
 41     string name;
 42     int weight;
 43   };
 44
 45  vector<Vertex> vertices;
 46  map <string name, list<Neighbor> > adj_list;
 4
 48 };
 49
 50 #endif

Graph.cpp Graph.cpp

 5 #include"MSTapp.h"
 6 #include"Graph.h"
 7
 8 #include<iostream>
 9 #include<map>
 10 #include<algorithm> // sort
 11 #include<string>
 12 #include<vector>
 13 #include<list>
 14
 15 using namespace std;
 35 void Graph::add_vertex(string name)
 36 {
 37  Vertex v1;
 38  v1.name = name;
 39  v1.pi = "NIL";
 40  v1.key = 100;
 41  bool check;
 42
 43  for(int i = 0; i < vertices.size(); i++)
 44  {
 45   if(vertices[i].name == v1.name)
 46   {
 47    check = true;
 48   }
 49  }
 50
 51  if(check == false)
 52  {
 53   vertices.push_back(v1);
 54  }
55 }
56
57 void Graph::print_vertices()
58 {
59  for(int i = 0; i < vertices.size(); i++)
60  {
61   cout << vertices[i].name << " " << vertices[i].pi << " " << vertices[i].key << endl;
62  }
63 }
64
65 void Graph::add_edge(string from, string to, int weight)
66 {
67  string v_from = from;
68  string v_to = to;
69  int v_weight = weight;
70
71  Neighbor n1;
72  n1.name = v_to;
73  n1.weight = v_weight;
74
75  adj_list[v_from];
76  adj_list[v_from].push_back(n1);
77  adj_list[v_from].sort();
78 }

All the functions for the Vertices are working so just ignore those. 顶点的所有功能都在起作用,因此请忽略这些功能。 The main problem I am having is the add_edge function in the Graph. 我遇到的主要问题是Graph中的add_edge函数。 For example my input would be something as follows: 例如,我的输入将如下所示:

A B C D E //Vertices, this is working
A B 3

so, with the AB 3, I would like to add the B and 3 the the neighbor, and do something like adj_list[A].push_back(Neighbor) 因此,对于AB 3,我想将B和3添加为邻居,并执行adj_list [A] .push_back(Neighbor)

The errors I am getting are regarding the map, it is saying I have the incorrect number of template arguments for the map in Graph.h. 我遇到的错误是关于地图的,它表示我在Graph.h中地图的模板参数数量不正确。

Am I declaring my map incorrectly on line 46 of Graph.h? 我在Graph.h的第46行上错误地声明了我的地图吗?

If anyone could provide any insight as to what I am doing wrong or how I can get this working it would be GREATLY appreciated. 如果有人能提供有关我做错了什么或如何使它起作用的任何见解,将不胜感激。 If any additional clarifcation is needed, just ask. 如果需要任何其他澄清,只需询问。 Thanks! 谢谢!

您应将地图声明为:

map <string, list<Neighbor> > adj_list;

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

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