如何对类型的 Vector 进行排序?

发布于 2024-11-08 12:34:22 字数 2960 浏览 0 评论 0原文

可能的重复:
C++ 如何对向量进行排序与运算符 <

大家好!

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

头文件:(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 是该类的对象)。

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

预先感谢您的帮助!任何建议或想法将不胜感激!

Possible Duplicate:
C++ how to sort vector<class *> with operator <

Hello everyone!

Please i am trying to sort a vector of type "class" based on one of its data members. As follows:

The Header File: (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

The Implementation File: (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();
}

Now in the above code, after creating several nodes and edges, i need to sort the edges of this graph based on their weight. I am studying a way to implement Kruskal algorithm so i though of sorting the edges first based on their weight.

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

obviously does not work! since the vector edges_List is of type "Edge".
Assuming that (myGraph is an object of the class).

I was wondering if there is any good technique to do that?

Thanks in advance for your help! Any suggestions or ideas are greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你列表最软的妹 2024-11-15 12:34:22

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

所以:

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); 

By default, std::sort uses using operator<, but you can also supply a comparison function object.

So:

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); 
2024-11-15 12:34:22

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

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

Another way is usage std::set container for storing your objects. And you always will have sorted container. All you need is define operator < for stored classes.

Usage vector with push_back is not effective way in terms of time. It cause unnecessary memory rellocations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文