0

我得到了要工作的顶点列表。现在我试图将边缘放入每个顶点,但是当我尝试将相邻顶点放入原始顶点列表中的边缘时,它将不起作用。我使用一个迭代器来查找边缘开始处的顶点,并使用另一个迭代器来查找边缘(街道)结尾处的顶点。出于某种原因,当我尝试将边缘的顶点指针设置为顶点列表中的元素时,它不允许这样做。这是错误“错误 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 file\n";
    }
    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
4

2 回答 2

0

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

于 2012-12-05T04:18:56.360 回答
0

迭代器的工作方式类似于指针,但它通常不是指针。你需要做类似的事情

tempe.adjvertex = &*tit;

*tit获取迭代器当前所在的顶点,因此&*tit获取指向该顶点的指针。

于 2012-12-05T03:56:16.937 回答