C++中文本文件的读写 我是一个非常新的C++程序员,试图实现以下代码: 两个给定节点之间的所有路径图。它使用连接边对和给定节点对计算它们之间的所有可能路径作为控制台的输入,并将给定节点对之间的所有可能路径写入控制台。该算法工作得非常好。但是,我希望从txt文件读取/写入输入/输出。但我做不到。有没有人给你指路 #include <stdio.h> #include <vector> #include <algorithm> #include <queue> #include <iostream> #include <fstream> using namespace std; vector<vector<int> >GRAPH(100); inline void printPath(vector<int>path) { cout<<"[ "; for(int i=0; i<path.size(); ++i) { cout<<path[i]<<" "; } cout<<"]"<<endl; }

C++中文本文件的读写 我是一个非常新的C++程序员,试图实现以下代码: 两个给定节点之间的所有路径图。它使用连接边对和给定节点对计算它们之间的所有可能路径作为控制台的输入,并将给定节点对之间的所有可能路径写入控制台。该算法工作得非常好。但是,我希望从txt文件读取/写入输入/输出。但我做不到。有没有人给你指路 #include <stdio.h> #include <vector> #include <algorithm> #include <queue> #include <iostream> #include <fstream> using namespace std; vector<vector<int> >GRAPH(100); inline void printPath(vector<int>path) { cout<<"[ "; for(int i=0; i<path.size(); ++i) { cout<<path[i]<<" "; } cout<<"]"<<endl; },c++,graph,path,io,C++,Graph,Path,Io,关于输出,您可以简单地使用std::of流,并在使用std::cout的地方替换它: 关于从文件中读取此格式,我建议使用s.th。像这样: std::ifstream ifs("MyGraphFile.txt"); while(ifs && !ifs.eof()) { std::string line = std::string::getline(ifs); // strip the '[' and ']' characters line = line.

关于输出,您可以简单地使用std::of流,并在使用std::cout的地方替换它:

关于从文件中读取此格式,我建议使用s.th。像这样:

std::ifstream ifs("MyGraphFile.txt");

while(ifs && !ifs.eof())
{
    std::string line = std::string::getline(ifs);
    // strip the '[' and ']' characters
    line = line.substr(1,line.length() - 2);
    std::istringstream iss(line);

    std::vector<int> currentInputs;
    int value;
    while(iss >> value)
    {
        currentInputs.push_back(value);
    }

    GRAPH.push_back(currentInputs);
    currentInputs.clear();
}

我认为这将是一个轻松的Boost图形库。而且,在某种程度上,确实如此

正在分析输入文件:

std::vector<std::pair<int, int>> parse_input(const char* const fname, int& min_vertex, int& max_vertex)
{
    std::vector<std::pair<int, int>> data;

    min_vertex = std::numeric_limits<int>::max();
    max_vertex = std::numeric_limits<int>::min();

    std::ifstream ifs("input.txt");
    std::string line;
    while (std::getline(ifs, line))
    {
        int a, b;
        if (std::istringstream(line) >> a >> b)
        {
            data.emplace_back(a, b);

            if (a>b) std::swap(a,b);
            min_vertex = std::min(min_vertex, a);
            max_vertex = std::max(max_vertex, b);
        }
        else throw "oops";
    }
    return data;
}
实际上,我们需要使用宽度优先访问,同时将每个顶点的颜色保留为白色,以生成所有路径

可悲的是,我没有时间尝试如何在访问合适的自定义颜色地图时,首先输入宽度


我希望这对输入解析有所帮助。

请详细说明您的问题。编译器错误、程序崩溃、意外行为……问题是我只想使用两个变量来分配输入。在这种情况下,如果我使用txt文件,我不能传递第二行输入,因为我将在第二行使用相同的变量。似乎,我需要一个循环,但不知道如何实现它。输入和输出是显示您想要的,还是显示您得到的,但与您想要的不同?我根本看不到std::fstream的任何用途?那个么你们想从文件中读/写数据到哪里呢?我使用了这些输入,得到了正确的输出。但我用的是控制台。。我想用txt文件读取输入和写入输出,我的代码做不到it@zaratushtra我只是试着给出一个示例,并没有真正编译它,我以为您会将其外推并集成到您的代码中。有什么特别不适合你??
int main()
{
    int T,totalNodes,totalEdges,u,v,sourceNode,targetNode;
    T=1;
    while(T--)
    {
        totalNodes=6;
        totalEdges=11;
        for(int i=1; i<=totalEdges; ++i)
        {
            scanf("%d%d",&u,&v);
            GRAPH[u].push_back(v);
        }
        sourceNode=1;
        targetNode=4;
        findpaths(sourceNode,targetNode,totalNodes,totalEdges);
    }
    return 0;
}
Input::
1 2 
1 3 
1 5 
2 1
2 3
2 4
3 4
4 3
5 6
5 4
6 3

output:

[ 1 2 4 ]
[ 1 3 4 ]
[ 1 5 4 ]
[ 1 2 3 4 ]
[ 1 5 6 3 4 ]
inline std::ostream& os printPath(std::ostream& os, vector<int>path)
{
    os <<"[ ";
    for(int i=0;i<path.size();++i)
    {
        os<<path[i]<<" ";
    }
    os<<"]"<<endl;
}

int main(int argc, char** argv)
{
    if(argc > 1)
    {
        std::ofstream ofs(argv{1]);

        // ...
        printPath(ofs,path)
    }
}
std::ifstream ifs("MyGraphFile.txt");

while(ifs && !ifs.eof())
{
    std::string line = std::string::getline(ifs);
    // strip the '[' and ']' characters
    line = line.substr(1,line.length() - 2);
    std::istringstream iss(line);

    std::vector<int> currentInputs;
    int value;
    while(iss >> value)
    {
        currentInputs.push_back(value);
    }

    GRAPH.push_back(currentInputs);
    currentInputs.clear();
}
std::vector<std::pair<int, int>> parse_input(const char* const fname, int& min_vertex, int& max_vertex)
{
    std::vector<std::pair<int, int>> data;

    min_vertex = std::numeric_limits<int>::max();
    max_vertex = std::numeric_limits<int>::min();

    std::ifstream ifs("input.txt");
    std::string line;
    while (std::getline(ifs, line))
    {
        int a, b;
        if (std::istringstream(line) >> a >> b)
        {
            data.emplace_back(a, b);

            if (a>b) std::swap(a,b);
            min_vertex = std::min(min_vertex, a);
            max_vertex = std::max(max_vertex, b);
        }
        else throw "oops";
    }
    return data;
}
struct my_visitor : boost::default_bfs_visitor 
{
    template < typename Vertex, typename Graph >
        void discover_vertex(Vertex const& u, const Graph & g) const
        {
            std::cout << "discover_vertex: " << u << "\n";
        }
};

int main() 
{
    typedef boost::adjacency_list<> Graph;
    typedef Graph::vertex_descriptor Vertex;

    int min_vertex, max_vertex;
    auto const lines = parse_input("input.txt", min_vertex, max_vertex);

    const Graph G(begin(lines), end(lines), max_vertex+1);
    print_graph(G);

    const auto source = vertex(min_vertex, G);

    breadth_first_search // visit ... needs explicit ColorMap
        (G, source, boost::visitor(my_visitor()));
}
0 --> 
1 --> 2 3 5 
2 --> 1 3 4 
3 --> 4 
4 --> 3 
5 --> 6 4 
6 --> 3 
discover_vertex: 1
discover_vertex: 2
discover_vertex: 3
discover_vertex: 5
discover_vertex: 4
discover_vertex: 6