C++ 如何使用集合交点和插入到集合中
我试图构建一类图,它由边和顶点表示 我已经将边实现为字符串集,将顶点实现为字符串集,并且希望创建方法,以便在创建的图形之间创建交点和并集。 这是一节课:C++ 如何使用集合交点和插入到集合中,c++,c++11,graph,set,intersection,C++,C++11,Graph,Set,Intersection,我试图构建一类图,它由边和顶点表示 我已经将边实现为字符串集,将顶点实现为字符串集,并且希望创建方法,以便在创建的图形之间创建交点和并集。 这是一节课: class Graph { public: string name; std::set<string> vertices; std::set<string> edges; //<v1,v2> format Graph()= default; Graph(const Gr
class Graph {
public:
string name;
std::set<string> vertices;
std::set<string> edges; //<v1,v2> format
Graph()= default;
Graph(const Graph&) = default;
~Graph() = default;
Graph graphUnion(const Graph& graph);
Graph graphIntersection(const Graph& graph);
};
请帮我识别这个错误,因为我不知道这里的问题是什么
谢谢大家! 你需要几样东西:
/*new_edges.end() =*/
std::set_intersection(this->edges.begin(), this->edges.end(),
graph.edges.begin(), graph.edges.end(),
std::inserter(new_edges, new_edges.begin()));
不需要设定终点。容器将自己管理它
您需要有一个插入器对象,它告诉您要添加到哪个集合以及在哪个位置。您需要一些东西:
/*new_edges.end() =*/
std::set_intersection(this->edges.begin(), this->edges.end(),
graph.edges.begin(), graph.edges.end(),
std::inserter(new_edges, new_edges.begin()));
不需要设定终点。容器将自己管理它
您需要有一个插入器对象,它告诉您要添加到哪个集合以及在哪个位置。.end()
返回一个迭代器,该迭代器引用
容器
因此,您不能分配给它(但可以与它进行比较)。您可能希望执行以下操作:
std::set<string>::iterator it = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end());
std::set::iterator it=std::set_交集(v1.begin()、v1.end()、v2.begin()、v2.end());
或
std::设置结果;
std::set_交叉点(v1.begin(),v1.end(),
v2.begin(),v2.end(),
插入器(result,result.begin());
.end()
返回一个迭代器,该迭代器引用
容器
因此,您不能分配给它(但可以与它进行比较)。您可能希望执行以下操作:
std::set<string>::iterator it = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end());
std::set::iterator it=std::set_交集(v1.begin()、v1.end()、v2.begin()、v2.end());
或
std::设置结果;
std::set_交叉点(v1.begin(),v1.end(),
v2.begin(),v2.end(),
插入器(result,result.begin());
分配给.end()
没有意义。@molbdnilo谢谢!你能解释一下原因吗?据我所知,set_intersection函数会将一个迭代器返回到新集合的末尾,因此将no?赋值给.end()
是毫无意义的。@molbdnilo谢谢!你能解释一下原因吗?据我所知,set_交集函数会在新集合的末尾返回一个迭代器,不是吗?
std::set<string> result;
std::set_intersection(v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::inserter(result, result.begin()));