在 C++ 中接受输入直到文件结束

taking input until end of file in c++

提问人:Md. Musfiqur Rahaman 提问时间:5/12/2021 更新时间:5/13/2021 访问量:208

问:

我正在解决一个图形问题,我必须从文件中获取输入。下面是我的输入.txt文件。

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

在上面的输入 .txt 文件中,第一个输入是顶点,其他直到文件末尾的输入是图形的定向边缘。第一个是源,第二个是目的地。所有输入都将从输入 .txt 文件中读取。

#include <bits/stdc++.h>
#include <fstream>
using namespace std;

class Graph {
    private:        
        int V;
        list<int> *l;

    public:
        Graph(int V) {
            this->V = V;
            l = new list<int>[V];
        }

        void addEdge(int source, int destination) {
            // As it is a directed graph edge will be source to destination only
            l[source].push_back(destination);
        }

        void printAdjList() {
            for(int i = 1; i <= V; i++) {
                cout << "Vertex " << i << "-> " ;
                for(int previous: l[i]) {
                    cout << previous << " " ;
                }
                cout << endl;
            }
        }
};



int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    ifstream inputFile;
    inputFile.open("input.txt");

    int noOfVertex, s, d, noOfEdge=20;
    inputFile >> noOfVertex ;
    // cout << noOfVertex << endl;

    Graph g(noOfEdge);

    // while(cin.eof()) {
    //     // cout << s << " " << d << endl;
    //     cin >> s >> d;
    //     g.addEdge(s, d);
    // }

    if(inputFile) {
        while(inputFile >> s >> d) {
            // cout << s << " " << d << endl;
            g.addEdge(s, d);
        }

        inputFile.close();
    }
    else {
        cout << "Error opening input file" << endl;
    }

    g.printAdjList();

    return 0;
}

运行代码后我得到这个结果

Vertex 1-> 2 
Vertex 2-> 3 4 5 
Vertex 3-> 6 
Vertex 4-> 5 7 
Vertex 5-> 2 6 7 
Vertex 6-> 3 8 
Vertex 7-> 8 10 
Vertex 8-> 7 
Vertex 9-> 7 
Vertex 10-> 9 11 
Vertex 11-> 12 
Vertex 12-> 10 
Vertex 13-> 
Vertex 14-> 
Vertex 15-> 
Vertex 16-> 
Vertex 17-> 
Vertex 18-> 
Vertex 19-> 

我不能为这个问题取边缘的数量。vetices 的数量和给定的有向边将从文件中一行一行地获取,它将显示这样的邻接列表

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

如何从文件中获取输入,以便获得上述输出?我已经应用了很多方法,但没有任何效果。

C++ 算法 输入 EOF

评论

0赞 Sneftel 5/12/2021
Graph 构造函数采用多个节点作为参数。为什么要传递边数?
1赞 user4581301 5/12/2021
不相关:表明您不知道什么。这里有一些关于这一点的阅读以及一个不那么微妙的建议:为什么我不应该 #include < bits/stdc++.h>?#include <fstream>#include <bits/stdc++.h>
0赞 user4581301 5/12/2021
硬编码只会导致痛苦和折磨。幸运的是,它没有做任何有用的事情,你可以删除它。noOfEdge
0赞 Serge Ballesta 5/13/2021
与您的问题没有直接关系,但您是否知道数组索引在 C 语言中从 0 开始?

答:

1赞 ravenspoint 5/12/2021 #1

我建议你使你的addEdge方法更灵活,更易于使用。作为一个问题,如果源输入大于列表的大小,您的程序将崩溃。逻辑应该是:

if there is no source vertex, add it
if there is no destination vertex, add it
add link from source to destination.

以下是建议程序的更详细说明。

/// find vertex "n", returning vertex index, or -1 if missing
 int find( int n )
{
  loop over graph
     if vertex is "n
        return index
  return -1
}

/// find vertex "n", or add it if not present
int findoradd( int n )
{
   int i = find( n );
   if( i >= 0 )
      return i
   return addvertex( n )
}

/// add link between vertices
void addLink( int u, int v )
{
    addEdge(
       findoradd( u ),
       findoradd( v ) );
}

评论

0赞 Md. Musfiqur Rahaman 5/13/2021
我怎样才能在代码中做到这一点?你能用代码教我吗?这将是非常有帮助的。
1赞 Serge Ballesta 5/13/2021 #2

您的代码错误有两个原因:

  • 您使用硬编码的边数,何时应使用顶点数
  • 当从文件中读取顶点的编号并且不从 0 开始时,您使用的数组的索引从 0 开始

为了安全起见,你应该使用映射(或无序映射)int -> list:

...
class Graph {
private:
    int V;
    unordered_map<int, list<int> > l;   // change here

public:
    Graph(int V) {
        this->V = V;
        l.reserve(V);                   // here
    }
...
    int noOfVertex, s, d, noOfEdge = 20;
    inputFile >> noOfVertex;
    // cout << noOfVertex << endl;

    Graph g(noOfVertex);                // and here
...

这足以达到预期:

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

评论

0赞 Md. Musfiqur Rahaman 5/13/2021
我真的很感谢你试图理解我的问题。