【问题标题】:How to sort a Vector of type <class*>? [duplicate]如何对 <class*> 类型的向量进行排序? [复制]
【发布时间】:2011-05-17 21:25:49
【问题描述】:

可能重复:
C++ how to sort vector<class *> with operator <

大家好!

请我尝试根据其中一个数据成员对“类”类型的向量进行排序。如下:

头文件:(Undirected_Graph.h)

#ifndef UNDIRECTED_GRAPH_H
#define UNDIRECTED_GRAPH_H

#include <vector>
using std::vector;

#include <climits>

class Edge;

class Node
{
    public:

        Node(int);                 //The constructor.

        int id;                    //For the id of the node.
        bool visited;              //For checking visited nodes.
        int distance;

        vector <Edge*> adj;       //The adjacent nodes.
};

class Edge
{
    public:

        Edge(Node*, Node*, int);   //The constructor.

        Node* start_Node;          //The start_Node start of the edge.
        Node* end_Node;            //The end of the edge.
        int w;                     //The weight of the edge.

        bool isConnected(Node* node1, Node* node2)  //Checks if the nodes are connected.
        {
            return((node1 == this->start_Node && node2 == this->end_Node) ||
                   (node1 == this->end_Node   && node2 == this->start_Node));
        }
};

class Graph
{
    public:

        Graph(int);                    //The Constructor.

        int max_Nodes;                 //Maximum Number of allowed Nodes.

        vector <Edge*> edges_List;     //For storing the edges of the graph.
        vector <Node*> nodes_List;     //For storing the nodes of the graph.

        void insertEdge(int, int, int);

        int getNumNodes();
        int getNumEdges();
};

#endif

实施文件:(Undirected_Graph.cpp)

#include "Undirected_Graph.h"

Node::Node(int id_Num)
{
    id = id_Num;
    visited = 0;
    distance = INT_MAX;
}

Edge::Edge(Node* a, Node* b, int weight)
{
    start_Node = a;
    end_Node = b;
    w = weight;
}

Graph::Graph(int size)
{
    max_Nodes = size;

    for (int i = 1; i <= max_Nodes; ++i)
    {
        Node* temp = new Node(i);

        nodes_List.push_back(temp);
    }
}

void Graph::insertEdge(int x, int y, int w)
{
    Node* a = nodes_List[x-1];
    Node* b = nodes_List[y-1];

    Edge* edge1 = new Edge(a, b, w);
    Edge* edge2 = new Edge(b, a, w);

    edges_List.push_back(edge1);

    a->adj.push_back(edge1);
    b->adj.push_back(edge2);
}

int Graph::getNumNodes()
{
    return max_Nodes;
}

int Graph::getNumEdges()
{
    return edges_List.size();
}

现在在上面的代码中,在创建了几个节点和边之后,我需要根据它们的权重对该图的边进行排序。我正在研究一种实现 Kruskal 算法的方法,所以我首先根据边缘的权重对边缘进行排序。

sort (myGraph.edges_List[index].begin(), myGraph.edges_List[index].end()); 

显然不行!因为向量edges_List 的类型是“Edge”。 假设(myGraph 是该类的一个对象)。

我想知道是否有任何好的技术可以做到这一点?

提前感谢您的帮助!非常感谢任何建议或想法!

【问题讨论】:

    标签: c++ sorting vector


    【解决方案1】:

    默认情况下,std::sort 使用 operator&lt;,但您也可以提供比较函数对象。

    所以:

    bool your_comparer(const Edge * left, const Edge * right)
    {
        // return true if left should come before right, false otherwise
    }
    
    // ...
    
    sort (myGraph.edges_List.begin(), myGraph.edges_List.end(), your_comparer); 
    

    【讨论】:

    • 感谢“Etienne de Martel”,它运行良好!
    【解决方案2】:

    另一种方法是使用 std::set 容器来存储您的对象。而且你总是会有分类容器。您只需为存储的类定义operator &lt;

    就时间而言,使用带有 push_back 的向量不是有效的方法。它会导致不必要的内存重新分配。

    【讨论】:

    • @mbykov 大多数实现分配的内存比必要的多,以避免不必要的重新分配。
    • @mbykov: -1 因为最后一个声明是错误的。 push_back 应该在amortized constant time 内实现,也就是说,基本上是O(1)。它平衡了这一点。
    • 要完成上面@Etienne 的评论,所有实现(如果符合)都必须在摊销常数时间内实现push_back,这需要以指数方式分配额外空间。
    • 看看 push_back 向量的实现。搬迁可能会发生(在大多数实施中并非每次都发生,但是......)。如果您将广泛使用 push_back,这一点很重要。
    • @mbykov std::vector 中的分配相对较少。 std::set 的大多数实现都使用二叉树,这意味着更多的分配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2019-12-24
    • 1970-01-01
    相关资源
    最近更新 更多