简体   繁体   English

如何转换矢量<pair<string, t> &gt; 到矢量<pair<string,string> &gt; </pair<string,string></pair<string,>

[英]How to convert vector<pair<string, T>> to vector<pair<string,string>>

I have a method that requires me to return a vector<pair<string, string>>.我有一个方法需要我返回一个向量<pair<string, string>>。 However, the input I have is of vector <pair<string, T>> .但是,我的输入是向量<pair<string, T>> This is part of a bigger question to return the number of edges in a graph.这是返回图中边数的更大问题的一部分。 The relevant code is down below where T is an integer type.相关代码在下面,其中T是 integer 类型。

map<string, string> all_vertices;
map<string, map<string, T>> adj_list;

template<typename T>
size_t Graph<T>::num_edges() {
    int edge_num = 0;

    for(auto& node : adj_list) {
        for(auto& edge : node.second) {
            edge_num++;
        }
    }

    return edge_num / 2;
}

template<typename T>
void Graph<T>::add_edge(const string& u, const string& v, const T& weight) {
    if(!adjacent(u, v)) {
        if(adj_list[u].find(v) == adj_list[u].end()) {
            adj_list[u].insert({v, weight});
            adj_list[v].insert({u, weight});
        }
    }
}

template <typename T>
void Graph<T>::add_vertex(const string& u) {
    
        if(!contains(u)){
            all_vertices.insert({u,u}); 
            adj_list[u]= map<string, T>();
     }
    
}

template <typename T>
bool Graph<T>::adjacent(const string& u, const string& v) {

     if(contains(u) && contains(v)){
        if (adj_list[u].find(v)!=adj_list[u].end()){
            return true;
        }
    }
    return false;
    
}

template<typename T>
vector<pair<string, string>> Graph<T>::get_edges() {
    vector<pair<string, T>> e;
    vector<pair<string, string>> e_string;
    for(auto x1 : adj_list) {
        for(auto x2 : x1.second) {
            e.push_back(x2);
        }
    }

    return e;
}
/* the test cases for the nodes in the graphs:

Number of vertices: 5

Number of edges: 8

Is vertex A in the graph? 1

Is vertex F in the graph? 0

Is there an edge between A and B? 1

Is there an edge between B and C? 0

*/

Description for the different functions:不同功能的说明:

  • vector<pair<string,string>> get_edges(); returns a vector of all the edges in the graph -- each edge is represented by a pair of vertices incident to the edge.返回图中所有边的向量——每条边由一对入射到边上的顶点表示。

  • void add_edge(const string&, const string&, const T&); adds a weighted edge to the graph -- the two strings represent the incident vertices;向图中添加加权边——两个字符串代表入射顶点; the third parameter represents the edge's weight.第三个参数表示边缘的权重。

I suggest that you skip the intermediate step of creating e and go directly to e_string .我建议你跳过创建e和 go 的中间步骤直接到e_string Also, make get_edges() const since you're not changing *this .另外,将get_edges() const ,因为您没有更改*this

Use std::to_string to convert the integer T to a std::string .使用std::to_string将 integer T转换为std::string

The real problem is however that your code tries to add only one vertex + the weight for each edge.然而,真正的问题是您的代码尝试仅添加一个顶点+ 每条边的权重。

In your description of the problem it says that it should be a pair of vertices.在您对问题的描述中,它说它应该是一对顶点。 It doesn't say that the weight should be included.它并没有说应该包括重量。

So, here's how that can be done:所以,这是如何做到的:

template <typename T>
std::vector<std::pair<std::string, std::string>> Graph<T>::get_edges() const {
    std::vector<std::pair<std::string, std::string>> e_string;
 
    for (auto&[u, Map] : adj_list) {
         // only add one direction, get all v's where u < v:
        for(auto vw_it = Map.upper_bound(u); vw_it != Map.end(); ++vw_it) {
            e_string.emplace_back(u, vw_it->first);
        }
    }
    
    return e_string;
}

Demo演示


Unrelated suggestions:不相关的建议:

You could simplify the num_edges() method (which should also be const ).您可以简化num_edges()方法(也应该是const )。 You now loop over all the elements in the inner map to count them, but the map has a size() member function, so that is not necessary:您现在遍历内部map中的所有元素来计算它们,但是map有一个size()成员 function,所以这不是必需的:

#include <numeric>  // std::accumulate

template<typename T>
size_t Graph<T>::num_edges() const {
    return
        std::accumulate(adj_list.begin(), adj_list.end(), size_t{0},
            [](size_t sum, const auto& node) {
                return sum + node.second.size();
            }) / 2;
}

In add_edge you could skip checking if the connection is already there and just try to add it.add_edge ,您可以跳过检查连接是否已经存在并尝试添加它。 It'll be rejected if it's a duplicate:如果它是重复的,它将被拒绝:

template<typename T>
void Graph<T>::add_edge(const std::string& u, const std::string& v,
                        const T& weight)
{
    if(u == v) return; // don't allow linking a vertex with itself
    adj_list[u].emplace(v, weight);
    adj_list[v].emplace(u, weight);
}

You may still want to have the adjacent function.您可能仍希望拥有adjacent的 function。 In that case I'd simplify it to something like this:在这种情况下,我会将其简化为以下内容:

template <typename T>
bool Graph<T>::adjacent(const string& u, const string& v) const {
    auto it = adj_list.find(u);
    return it != adj_list.end() && it->second.find(v) != it->second.end();

    // return it != adj_list.end() && it->second.contains(v); // C++20
}

Another possible solution, without modifying your code too much, would be to convert T to string at input.另一种可能的解决方案是在不过多修改代码的情况下将 T 转换为输入字符串。

#include <iostream>
#include <vector>
#include <string>
#include <map>

using std::vector;
using std::string;
using std::map;
using std::pair;

map<string, string> all_vertices;

template <typename T>
map<string, map<string, T>> adj_list;

template <typename T>
vector<pair<string, string>> Graph<T>::get_edges() const {

   vector<pair<string, T>> e;

   for (auto x1 : adj_list<T>) {
      for (auto x2 : x1.second) {
         e.push_back(x2);

      }
   }

   return e;
}

// Graph<string> g;
// vector<pair<string, string>> edges = g.get_edges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM