使用C++中的列表制作图表
making a graph using lists in C++
我得到了顶点列表的工作。现在我正在尝试将边放入每个顶点,但是当我尝试将相邻的顶点放入原始顶点列表中的边缘时,它将不起作用。我使用一个迭代器在边缘的开头查找顶点,使用另一个迭代器在边缘(街道)的末端查找顶点。出于某种原因,当我尝试将边缘的顶点指针设置为顶点列表中的元素时,它不允许这样做。下面是错误"错误 1 错误 C2679:二进制'=':找不到采用类型为"std::_List_iterator<_Mylist>"的右操作数的运算符":
主要:
#include<iostream>
#include<list>
#include <fstream>
#include<cmath>
#include <cstdlib>
#include <string>
#include<sstream>
#include "global.h"
#include "edge.h"
#include "vertex.h"
using namespace std;
int main ( int argc, char *argv[] ){
if(argc != 4){
cout<< "usage: "<< argv[0]<<"<filename>n";
}
else{
ifstream map_file (argv[3]);
if(!map_file.is_open()){
cout<<"could not open filen";
}
else{
Edge tempe;
Vertex tempx;
std::string line;
std::ifstream input(argv[3]);
int xsect;
int safety;
int change = 0;
int i = 0;
int vert = 0;
int adjvert = 0;
int dist = 0;
//k and m keep track of iteration through list
int k = 0;
int m = 0;
std:string xname;
std::list<Vertex> xSectionList;
std::list<Edge> EdgeList;
while (getline(input, line))
{
std::istringstream iss(line);
if(line == "INTERSECTIONS:"){
change =1;
i = 0;
}
else if(line == "STREETS:"){
change = 2;
i = 0;
}
if( change ==1 && i != 0){
iss >> xsect >> safety;
std::getline(iss, xname);
tempx.xsect = xsect;
tempx.danger = safety;
tempx.xstreets = xname;
xSectionList.push_back(tempx);
}
else if(change ==2 && i!=0){
iss >> vert >> adjvert >> dist;
std::list<Vertex>::iterator it = xSectionList.begin();
while(it->xsect != vert || k != xSectionList.size()+1) {
it++;
k++;
}
std::list<Vertex>::iterator tit = xSectionList.begin();
while(tit->xsect != adjvert || m != xSectionList.size()+1) {
tit++;
m++;
}
//tempe.adjvertex = tit;
tempe.distance = dist;
it->EdgeList.push_back(tempe);
it->EdgeList.begin()->adjvertex = tempx;
//std::getline(iss, xname);
}
//cout<<xsect<< " " << safety << " " << xname<<endl;
i++;
}
std::list<Vertex>::iterator kit = xSectionList.begin();
std::list<Edge>::iterator lit = kit->EdgeList.begin();
//std::cout << it->xsect<< " " << it->danger << " "<< it->xstreets;
for(lit = kit->EdgeList.begin(); lit != kit->EdgeList.end(); lit++) {
cout << kit->xsect << "" << lit->adjvertex->xsect << "" << lit->distance<< endl;
}
}
}
getchar();
}
标题顶点'
#ifndef VERTEX_H
#define VERTEX_H
#include<list>
#include<string>
#include "global.h"
struct Vertex_{
int xsect;
int danger;
std::string xstreets;
std::list<Edge> EdgeList;
/*struct Vertex_ *next;
struct Vertex_ *prev;*/
};
#endif
边缘标头
#ifndef EDGE_H
#define EDGE_H
#include "global.h"
#include "vertex.h"
struct Edge_{
Vertex *adjvertex;
int distance;
/*struct edge *next;
struct edge *prev;*/
};
#endif
文件示例 我正在扫描数据,其中交叉点是顶点,街道是边:
397 0.6 Drachman and 27th
398 0.5 Drachman and 28th
399 0.3 Drachman and 29th
400 0.8 Drachman and 30th
STREETS:
1 2 0.5
2 3 1.0
3 4 0.9
4 5 0.7
5 6 0.1
迭代器的工作方式类似于指针,但它通常不是指针。 你需要做一些类似的事情
tempe.adjvertex = &*tit;
*tit
获取迭代器当前所在的顶点,因此&*tit
获取指向该顶点的指针。
除非这是家庭作业,否则我只会使用提升图。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题