温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c++ - Reading graph from stream to vector of adjacency list
c++ data-structures vector graph

c++ - 从流到邻接表的向量读取图

发布于 2020-03-28 23:33:08

我正在尝试通过读取下面显示的文件来创建图作为邻接表

这里的第一行是顶点数。在下面的示例中,我们有12个顶点,在以下几行中,例如1 2 3 4表示顶点1的边为2、3和4。

12
1 2 3 4
2 1 5 6
3 1 6
4 1 7
5 2 9 10
6 2 3
7 4 8 
8 4 7
9 5 10
10 5 9
11 12
12 11

C ++程序

#include <iostream>
#include <fstream>
#include <strstream>
#include <sstream> // for std::getline

#include "Graph.h"

using namespace std;

// Note: vector index is will match the vertex id.
std::vector <std::vector<std::pair<int, int> > > vecIdxedGraph;


void storeEdges(const string& strEdges) {
    std::istringstream iss(strEdges);
    int FromVertex = 0;
    iss >> FromVertex;

    std::cout << "From Vertex " << FromVertex << endl;

    vector<pair<int, int>> edges;
    string toVertex;
    while (std::getline(iss, toVertex, ' ')) {
        std::cout << "to Vertex " << toVertex << " ";
        edges.push_back(std::make_pair(atoi(toVertex.c_str()), 0));
    }
    std::cout << std::endl;
    vecIdxedGraph.push_back(edges);
    return;
}

void printGraph() {
    for (int i = 0; i < vecIdxedGraph.size(); i++) {
        cout << "Vertex " << i + 1 << " ";
        for (int j = 0; j < vecIdxedGraph[i].size(); j++) {
            cout << vecIdxedGraph[i][j].first << " ";
        }
        cout << endl;
    }
}
int main() {

    // create Graph
    ifstream inFile("Graph.txt");
    if (!inFile) {
        cout << "Open input file failed." << endl;
    }

    // Set input stream to file.
    std::streambuf* pOldCinBuf = cin.rdbuf();
    cin.set_rdbuf(inFile.rdbuf());

    int iNumberOfNodes;
    cin >> iNumberOfNodes;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Number of nodes in graph are: " << iNumberOfNodes << endl;

    vecIdxedGraph.reserve(iNumberOfNodes);



    for (int iVertexId = 1; iVertexId <= iNumberOfNodes; iVertexId++) {
        std::string vertIdDetails;
        std::getline(cin, vertIdDetails);
        // cout << (vertIdDetails.c_str()) << endl;
        // parse line and store to the vector.
        storeEdges(vertIdDetails);
        // cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    printGraph();
    return 0;
}

以下是输出

Vertex 1 0 2 3 4
Vertex 2 0 1 5 6
Vertex 3 0 1 6
Vertex 4 0 1 7
Vertex 5 0 2 9 10
Vertex 6 0 2 3
Vertex 7 0 4 8
Vertex 8 0 4 7
Vertex 9 0 5 10
Vertex 10 0 5 9
Vertex 11 0 12
Vertex 12 0 11

想要删除0。我认为读取行时0来自空白空间

while (std::getline(iss, toVertex, ' '))

我不知道如何清除它。

另一个问题是如何创建具有默认构造的向量,以便可以使用

vecIdxedGraph [FromVertex] = ...,而不是push_back。

请帮助

查看更多

查看更多

提问者
venkysmarty
被浏览
108
P0W 2020-01-31 18:28

看起来您已经使这里的事情变得复杂了。您只需要以下内容。

// Some handy names
using Pair = std::pair<int, int>;
using Graph = std::vector<std::vector<Pair>> ;

// The stream handle
std::ifstream fileHandle("Graph.txt");

size_t iNumberOfNodes  = 0;

// 1. Get # of Vertices
fileHandle >> iNumberOfNodes;

// 2. Create a graph, pre allocate the # of vertices
// One node extra since your vertices starts from 1
Graph g{iNumberOfNodes + 1, std::vector<Pair>{}}; 

std::string lineStr;
int currNode = 0, vertex;
// 3. Iterate over file, reading line by line
while ( std::getline(fileHandle, lineStr) ) {
   std::stringstream ss{lineStr};
    ss >> currNode;
    while ( currNode != 0 && ss >> vertex ) {
        g[currNode].emplace_back( vertex, 0 ) ; // weight = 0;
    }   
 }

你们都准备好了! Demo Here