【发布时间】:2020-08-10 19:09:05
【问题描述】:
我不断收到以下代码的此错误。
阅读 this 后,我认为我的错误是我的 for 循环中的 it++,我尝试用 next(it, 1) 替换它,但它并没有解决我的问题。
我的问题是,迭代器是给我带来问题的那个吗?
#include <iostream>
#include <vector>
#include <stack>
#include <set>
using namespace std;
struct Node
{
char vertex;
set<char> adjacent;
};
class Graph
{
public:
Graph() {};
~Graph() {};
void addEdge(char a, char b)
{
Node newV;
set<char> temp;
set<Node>::iterator n;
if (inGraph(a) && !inGraph(b)) {
for (it = nodes.begin(); it != nodes.end(); it++)
{
if (it->vertex == a)
{
temp = it->adjacent;
temp.insert(b);
newV.vertex = b;
nodes.insert(newV);
n = nodes.find(newV);
temp = n->adjacent;
temp.insert(a);
}
}
}
};
bool inGraph(char a) { return false; };
bool existingEdge(char a, char b) { return false; };
private:
set<Node> nodes;
set<Node>::iterator it;
set<char>::iterator it2;
};
int main()
{
return 0;
}
【问题讨论】:
-
链接是关于
operator+,但是你在你的迭代器上使用operator++,细节很重要。无论如何,每个迭代器都有operator++但您的问题与迭代器完全无关。正如错误所说,这是关于operator<。`
标签: c++ graph iterator c++-standard-library stdset