提问者:小点点

查找向量中是否已存在结构C++


我必须找到图中所有边之间的权重,所以由于边是双向的,我不想包含2->; 1如果我已经有1->; 2(因为它们的重量相同)。 边缘存储在结构edge的向量中。 我最初的想法是向上看,如果有一个边,有开始和结束位置交换和有相同的权重已经存在,如果是这种情况,只是不做任何事情。 但是,我不知道如何把它写进代码,所以任何帮助都是非常感激的。 此外,任何可以优化解决方案的方法也是受欢迎的。

struct Vertex {
    Vertex(const int i = 0) : index {i}, key {max_key}, parent_index {undef}, processed {false} {}

    int index;        // vertex identifier
    int key;          // temporary minimal weight (Prim algorithm)
    int parent_index; // temporary minimal distance neighboor vertex (Prim algorithm)
    int processed;    // flag used to mark vertices that are already included in V'

    static constexpr int max_key = std::numeric_limits<int>::max();
    static const int undef = -1;
};

struct Edge {
    Edge(int va, int vb, int w) : vi1 {va}, vi2 {vb}, weight {w} { }

    int vi1;    //start point
    int vi2;    //end point 
    int weight;
};

struct Graph {
    int N;                    // number of vertices
    std::vector<Vertex> V;    // set of vertices
    std::vector<Edge> E;      // set of edges
    std::vector<Edge> MST;    // minimal spanning tree
    const int* weights_table; // weights given as distance matrix
};

问题出在find中我知道这是很多不相关的代码,但是我发布它是为了让您更清楚地描述它。 如果两个顶点之间没有连接,则它们的权重为-1

// construct vertices and edges for a given graph
void createGraph(Graph& G) {
    // TODO 5.1a: clear V and E and insert all vertex objects and edge objects
    // - vertices are numbered (labeled) from 0 to N-1
    // - edges exist if and only if there is positive distance between two vertices
    // - edges are bidirectional, that is, edges are inserted only once between two vertices
    G.E.clear();
    G.V.clear();

    for(int i = 0; i < G.N; i++){
        Vertex V (i);
        G.V.push_back(V);
    }
  
    for(int i = 0; i < G.N; i++){
        for(int j = 0; j < G.N; j++){
            Edge Ed (i,j,0);
            int weight = getWeight(G,i,j);
            if(weight > 0){
                Ed.weight = weight;
                auto it = find(G.E.begin(), G.E.end(), ....);
                if( it != G.E.end() ) continue;
                G.E.push_back(Ed);
            }
        }
    }
}

谢啦!


共2个答案

匿名用户

因为边是双向的

您可以构造edges,使得v1<=v2,那么每个可能的边只有一种表示形式。

struct Edge {
    Edge(int va, int vb, int w) : vi1 {std::min(va, vb)}, vi2 {std::max(va, vb)}, weight {w} { }

    int vi1;    // earlier point
    int vi2;    // later point
    int weight;
};

旁白:首选就地构造边缘

for(int i = 0; i < G.N; i++){
    for(int j = G.N - 1; j >= 0 + i; j--){
        int weight = getWeight(G,i,j);
        if(weight > 0){
            G.E.emplace_back(i, j, weight);
        }
    }
}    

匿名用户

好的,我想我明白了,把第二个for循环改成这样,但我也很好奇,如果使用find,语法会是什么样子


    for(int i = 0; i < G.N; i++){
        for(int j = G.N - 1; j >= 0 + i; j--){
            Edge Ed (i, j , 0);
            int weight = getWeight(G,i,j);
            if(weight > 0){
                Ed.weight = weight;
                G.E.push_back(Ed);
            }
        }
    }    

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(查找|向量|中|结构|c++)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?