Hi,
Visual Studio seems to be telling me that the iterator it is const (and therefor not letting me return it) although I can't seem to figure out why?
Anyone know why and what I can do to change that?
Thanks
1 2 3 4 5 6 7 8 9 10 11 12
Vertex* Graph::findVertex(string s)
{
map<Vertex, list<Vertex>>::iterator it;
for (it = graphMap.begin(); it != graphMap.end(); it++)
{
if (it->first.Key == s)
return it;
}
returnnullptr;
}
Here's all of my code so far.
NOTE: There are parts missing as I am still in the process of writing it. I am just posting everything in case something that I wrote elsewhere directly corelates with this issue (the iterator being const).
const Vertex* findVertex(const string& s)
{
for (auto it = graphMap.begin(); it != graphMap.end(); ++it)
if (it->first.Key == s)
return &(it->first);
returnnullptr;
}
or even:
1 2 3 4 5 6 7 8
const Vertex* findVertex(const string& s)
{
for (constauto& [ver, lst] : graphMap)
if (ver.Key == s)
return &ver;
returnnullptr;
}
Why return a pointer to a key in a map? Why not just return the iterator - with .end() if not found?
1 2 3 4 5 6 7 8
auto findVertex(const string& s)
{
for (auto it = graphMap.begin(); it != graphMap.end(); ++it)
if (it->first.Key == s)
return it;
return graphMap.end();
}
The real problem is the way you're representing the Vertices. What is graphMap supposed to represent? Since it maps a vertex to a list of vertices, does it represent the edges from the key vertex? But then shouldn't it map to a list of pointers to vertices? Otherwise you can't have more than one edge into a vertex.
You'll never get the code right unless you're crystal clear on what the data represents. This is why it's a good idea to comment the data. It lets you put a stake in the group to declare what the data represents.
Your graph needs a collection of vertices with two key properties:
- They can't move, since each Edge contains a pointer to the vertex
- You want quick access by vertex string.
So maybe something like this:
map<string, unique_ptr<vertex> > vertices;
Then findVertex is just
1 2
auto iter = vertices.find(s);
return (iter == vertices.end() ? nullptr : iter->second.get());
it's a homework assignment so it's not entirely up to me.
-the class vertex has to have 2 fields
1) a string for a key
2) a list of edges (it's a directed graph)
-the class that represents the graph itself has to have a map where the key is a Vertex which is mapped to a list of neighbors (I understood that to mean a list of Vertices which either has an edge to our Vertex or our Vertex has an edge to it)
shouldn't the following code return an iterator that would point to a level on the map?
based on the error from the second piece of included code it seems to be returning a Vertex and not a pointer to that level?
1 2 3 4 5 6 7 8
map<Vertex, list<Vertex>>::iterator Graph::findVertex(const string& s)
{
for (map<Vertex, list<Vertex>>::iterator it = graphMap.begin(); it != graphMap.end(); ++it)
if (it->first.Key == s)
return it;
return graphMap.end();
}
1 2 3 4 5 6 7
bool Graph::delVertexShell(string v)
{
map<Vertex, list<Vertex>>::iterator temp = (findVertex(v))->first; //find the Vertex that needs to be deleted (this line causes the error)
// delete edges that are incident (to or from ) it
}
->first returns a type Vertex (type of map key), not an iterator so:
1 2 3 4 5 6 7 8 9 10
bool delVertexShell(const string& v)
{
const map<Vertex, list<Vertex>>::iterator temp = findVertex(v);
if (temp != graphMap.end()) {
const Vertex &vert = temp->first; //find the Vertex that needs to be deleted (this line causes the error)
// delete edges that are incident (to or from ) it
}
}
This is where auto comes into it's own!
1 2 3 4 5 6 7 8 9 10
bool delVertexShell(const string& v)
{
constauto temp = findVertex(v);
if (temp != graphMap.end()) {
constauto& vert = temp->first; //find the Vertex that needs to be deleted (this line causes the error)
// delete edges that are incident (to or from ) it
}
}
Complain long and loud about being forced to use an extremely out-of-date C++ compiler! It sounds like you're using a C++98 compiler - which is 22 years out of date! The current C++ standard is C++20!
lol. I'm in second year and we've been complaining about that for more than a year and a half. All of the professors are on our side but the IT department keeps on saying that they can't update it on the online checker for technical reasons. I'm not sure why.
I'm pretty sure that you're right that it's a C++98 compiler. I've had to rewrite a lot of homework assignments after I tried submitting them and they don't even get compiled despite working perfectly on Visual Studio.
either way I can't seem to figure out how to delete a Vertex from the graph.
my thought process is as follows:
1) for each Vertex in the neighbor list (the second field in the map) check if it's a directed edge from our Vertex using the targetExist function (our vertex is the source).
2) if it's not, then access that neighbor Vertex through the neighbor list in the map and remove that edge from his edgeList .
3) remove the intended Vertex from the graphMap
I've been having a lot of issues with the implementation though (as you can see above)