使用C++中的列表制作图表

making a graph using lists in C++

本文关键字:列表 C++ 使用      更新时间:2023-10-16

我得到了顶点列表的工作。现在我正在尝试将边放入每个顶点,但是当我尝试将相邻的顶点放入原始顶点列表中的边缘时,它将不起作用。我使用一个迭代器在边缘的开头查找顶点,使用另一个迭代器在边缘(街道)的末端查找顶点。出于某种原因,当我尝试将边缘的顶点指针设置为顶点列表中的元素时,它不允许这样做。下面是错误"错误 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获取指向该顶点的指针。

除非这是家庭作业,否则我只会使用提升图。