C++ 如何将对象插入到集合中?
我试图创建一个图,我试图通过创建一组类型顶点和一组类型边来实现这一点。但是,我遇到了以下错误: 二进制表达式('const-Vertex'和'const-Vertex'的操作数无效) 甚至可以在集合中保存对象吗?我不是一个优秀的程序员,这是为了我的大学项目。如果您看到**周围的代码块,那是因为我尝试将这些部分加粗以尝试突出显示,但没有成功。这些**不在我的实际代码中C++ 如何将对象插入到集合中?,c++,C++,我试图创建一个图,我试图通过创建一组类型顶点和一组类型边来实现这一点。但是,我遇到了以下错误: 二进制表达式('const-Vertex'和'const-Vertex'的操作数无效) 甚至可以在集合中保存对象吗?我不是一个优秀的程序员,这是为了我的大学项目。如果您看到**周围的代码块,那是因为我尝试将这些部分加粗以尝试突出显示,但没有成功。这些**不在我的实际代码中 class Vertex { public: Vertex(); Vertex(std::string);
class Vertex
{
public:
Vertex();
Vertex(std::string);
std::string getVertexName();
private:
std::string name;
};
class Edge{
Edge();
Edge(std::string, std::string);
std::string source;
std::string destination;
};
class Graph{
public:
void buildGraph( );
void createVertex(std::string vertexName);
// **
std::set <Vertex> setofvertices;
std::set <Vertex>::iterator itervertex = setofvertices.begin();
std::set <Edge> setofedges;
std::set <Edge>::iterator iteredge = setofedges.begin();
// **
};
Graph* DataSource:: buildGraph()
{
Graph *createdGraph;
//Vertex *createdVertex;
std::ifstream inputFile;
std::string followee, follower;
inputFile.open(this->filename);
if (inputFile.is_open()){
std::stringstream line;
std::string fileLine;
createdGraph = new Graph();
while(true){
getline(inputFile, fileLine);
if (inputFile.eof()) break;
line.clear();
line.str(fileLine);
line >> followee >> follower;
std::cout << followee << " " << follower << std::endl;
// **
createdGraph->setofvertices.insert(followee);
createdGraph->setofvertices.insert(follower);
createdGraph->setofedges.insert(followee,follower);
// **
}
return createdGraph;
}
类顶点
{
公众:
顶点();
顶点(std::string);
std::字符串getVertexName();
私人:
std::字符串名;
};
阶级边缘{
边();
边(std::string,std::string);
std::字符串源;
std::字符串目的地;
};
类图{
公众:
void buildGraph();
void createVertex(std::string vertexName);
// **
std::顶点集;
std::set::iterator itervertex=setofvertices.begin();
标准:一组边;
std::set::iterator iteredge=setofedges.begin();
// **
};
Graph*数据源::buildGraph()
{
图形*createdGraph;
//顶点*createdVertex;
std::ifstream输入文件;
std::字符串跟随者,跟随者;
打开(此->文件名);
if(inputFile.is_open()){
std::stringstreamline;
std::字符串文件行;
createdGraph=新图形();
while(true){
getline(inputFile,fileLine);
如果(inputFile.eof())中断;
line.clear();
str(文件行);
行>>跟随者>>跟随者;
std::cout错误是因为集合中的项目被排序,而顶点
和边缘
没有提供这样的机制。最简单的方法是实现操作符错误是因为集合中的项目被排序,而顶点
和边缘
没有提供这样的机制。最简单的方法是这样做是为了实现操作符我收到了这个错误:
请发布完整的错误消息。放置在集合中的对象需要进行排序。您需要提供操作符可以将对象存储在集合中。但您做得不对。阅读一本书可能是我能给出的最好建议。@KamilCuk抱歉,我附加了一个操作符如果有帮助,请在底部添加一个屏幕截图。屏幕截图表明错误正如退休忍者所说。我收到了这个错误:
请发布完整的错误消息。放置在集合中的对象需要排序。您需要提供一个操作。可以将对象存储在集合中。但您做得不对。请阅读一本书可能是我能给出的最好的建议。@KamilCuk抱歉,如果有帮助的话,我在底部附上了一个屏幕截图。屏幕截图表明错误正如退休忍者所说。哇,这太棒了,非常感谢你,我真的很感谢你的帮助。我也不知道eof,我会确保做出更改。哇,这太棒了,非常感谢你非常感谢你的帮助。我也不知道eof的事,我会确保做出改变。
#include <iostream>
#include <set>
#include <string>
#include <tuple>
class Vertex
{
public:
Vertex(std::string name_)
: name(name_)
{}
bool operator<(const Vertex &rhs) const
{
return name < rhs.name;
}
std::string name;
};
class Edge
{
public:
Edge(std::string source_, std::string destination_)
: source(source_), destination(destination_)
{}
bool operator<(const Edge &rhs) const
{
// Easy way to handle comparison of multiple variables
// https://en.cppreference.com/w/cpp/utility/tuple/tie
return std::tie(source, destination) < std::tie(rhs.source, rhs.destination);
}
std::string source;
std::string destination;
};
class Graph
{
public:
std::set <Vertex> setofvertices;
std::set <Edge> setofedges;
};
int main()
{
Graph g;
g.setofvertices.emplace(Vertex("a"));
g.setofvertices.emplace(Vertex("b"));
g.setofvertices.emplace(Vertex("c"));
// intentional duplicate that will be rejected
g.setofvertices.emplace(Vertex("c"));
g.setofedges.emplace(Edge("a", "b"));
// intentional duplicate that will be rejected
g.setofedges.emplace(Edge("a", "b"));
g.setofedges.emplace(Edge("b", "c"));
g.setofedges.emplace(Edge("c", "a"));
for (auto &v : g.setofvertices)
{
std::cout << "Vertex: " << v.name << "\n";
}
for (auto &e : g.setofedges)
{
std::cout << "Edge : " << e.source << " -> " << e.destination << "\n";
}
}
while(true){
getline(inputFile, fileLine);
if (inputFile.eof()) break;