Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要删除重复的顶点(邻接列表)_C++_Class_Stdvector_Adjacency List_Stdlist - Fatal编程技术网

C++ 我需要删除重复的顶点(邻接列表)

C++ 我需要删除重复的顶点(邻接列表),c++,class,stdvector,adjacency-list,stdlist,C++,Class,Stdvector,Adjacency List,Stdlist,文件test.txt 6(这是顶点数) 1 4(e)第一个是顶点的编号,第二个是构造边的顶点的编号 16 2.1 2 3 2 4 2.5 3.2 35 4.1 4.2 4.5 5.2 5.3 5.4 6.1 #include <iostream> #include <fstream> #include <vector> #include <list> using namespace std; class edge

文件test.txt

  • 6(这是顶点数)

  • 1 4(e)第一个是顶点的编号,第二个是构造边的顶点的编号

  • 16

  • 2.1

  • 2 3

  • 2 4

  • 2.5

  • 3.2

  • 35

  • 4.1

  • 4.2

  • 4.5

  • 5.2

  • 5.3

  • 5.4

  • 6.1

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <list>
    
    using namespace std;
    
    class edge
    {
    private:
    int node2id, nodeid;
    public:
    edge(int id, int id2)
    {
        node2id = id2;
        nodeid = id;
    
    }
    
    int getnodeid()
    {
        return nodeid;
    }
    int getnodeid2()
    {
        return node2id;
    }
    
    };
    
    int main()
    { 
    int totalnode, node1, node2;
    ifstream input("test.txt");
    input >> totalnode;
    vector<list<edge>>adjList(totalnode);
    while (input >> node1 >> node2)
    {
        adjList[node1 - 1].push_back(edge(node2, node1));
    }
    
    int c = 1;
    vector<list<edge>>::iterator i;
    for (i = adjList.begin(); i != adjList.end(); i++)
    {
        cout << c << " -- ";
        list<edge>  li = *i;
        list<edge>::iterator iter;
        for (iter = li.begin(); iter != li.end(); iter++)
        {
            cout << "[" << (*iter).getnodeid() << "] ";
        }
        cout << endl;
        c++;
    }
    
    while (input >> node1 >> node2)
    {
        adjList[node1 - 1].push_back(edge(node1, node1));
    }
    
    cout << "\n";
    
    这些顶点是创建的,但例如,顶点7和10(等等)具有相同的连接。您需要确保没有相同的顶点

    7 -- [1] [2]
    8 -- [1] [4]
    9 -- [1] [6]
    10 -- [2] [1]
    11 -- [2] [3]
    12 -- [2] [4]
    13 -- [2] [5]
    14 -- [3] [2]
    15 -- [3] [5]
    16 -- [4] [1]
    17 -- [4] [2]
    18 -- [4] [5]
    19 -- [5] [2]
    20 -- [5] [3]
    21 -- [5] [4]
    22 -- [6] [1]
    
     
    

    根据您目前所拥有的,似乎您并不真正需要
    edge
    类。所有信息都可以存储在
    int



    为了解决您想要的问题,您可以首先将所有边存储在
    std::vector中。不确定我是否正确理解您的问题,但是不同的顶点都有连接到相同顶点的边,这并不意味着它们是同一个顶点。是的,我理解,我认为我的问题表述得很糟糕。但是如果我们将示例输出作为一个例子然后顶点7和10有相同的邻接列表。非常感谢!
    
    
    1 -- [2] [4] [6]
    2 -- [1] [3] [4] [5]
    3 -- [2] [5]
    4 -- [1] [2] [5]
    5 -- [2] [3] [4]
    6 -- [1]
    
    7 -- [1] [2]
    8 -- [1] [4]
    9 -- [1] [6]
    10 -- [2] [1]
    11 -- [2] [3]
    12 -- [2] [4]
    13 -- [2] [5]
    14 -- [3] [2]
    15 -- [3] [5]
    16 -- [4] [1]
    17 -- [4] [2]
    18 -- [4] [5]
    19 -- [5] [2]
    20 -- [5] [3]
    21 -- [5] [4]
    22 -- [6] [1]
    
     
    
    while (input >> node1 >> node2)
    {
        adjList[node1 - 1].insert(node2);
    }
    
    10 -- [2] [1]
    
    10 -- [1] [2]
    
    std::map<std::set<int>, int> tempMap;
    
    for(auto i = 0; i < totalnode; ++i)
    {
        s.try_emplace(adjList[i], i);
    }
    
    std::map<int, std::set<int>> adjMap;
    
    for(auto& [key, value] : tempMap)
    {
        adjMap.emplace(value, key);
    }
    
    for(auto& [node, edges] : adjMap
    {
        std::cout << node << " -- ";
        for(auto& edge: edges)
        {
            std::cout << "[" << edge << "]";
        }
        std::cout << "\n";
    }
    
    vector<list<edge>>::iterator i;
    for (i = adjList.begin(); i != adjList.end(); i++) { // your loop }
    
    for(auto it = adjList.begin(); it != adjList.end(); ++it) { // your loop }
        ^^^^
    
    for(auto& item : adjList) { // your loop }