【问题标题】:How to get the second pair value from vector pair container?如何从向量对容器中获取第二对值?
【发布时间】:2021-11-27 08:36:43
【问题描述】:

我想使用向量对容器中的第一个值来获取该对的第二个值。如何在向量对中查找和遍历,并使用第一个值对获取第二对值的值?

在代码中,“G.keys()”是一个向量对,第一个值是字符,下一个是字符串。

void search_shortest_path(){
    Graph<T> G = create_graph();
    T source;
    T destination;
    std::cout<<"AVAILABLE ROUTES\n";

    for(auto k: G.keys()){
        std::cout<<k.first<<". "<<k.second<<"\n";
    }

    std::cout<<"Enter your starting point: ";
    std::cin>>source;
    std::cout<<"Enter your destination point: ";
    std::cin>>destination;

    //GET SECOND PAIR VALUE USING SOURCE AND DESTINATION ?
    

    G.dijkstra_algorithm(source, destination);
    }

【问题讨论】:

  • 不清楚,你的代码不完整(你提到了一个字符和一个字符串,但我看到了T)?请发帖minimal reproducible example
  • 你能用std::findstd::find_if吗?

标签: c++ algorithm data-structures


【解决方案1】:

使用 std::map 代替,然后您可以找到与其键 iterator find (const key_type&amp; k) std::map::find 关联的任何元素

【讨论】:

  • 当然可以,但可能 OP 对输入容器有限制,无法更改。
  • 谢谢!我已经转换了,比使用pair更容易!
猜你喜欢
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 2022-11-20
  • 1970-01-01
  • 2014-02-12
相关资源
最近更新 更多